Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有必要检查数据集中是否存在列_Python_Pandas_Dataframe - Fatal编程技术网

Python 是否有必要检查数据集中是否存在列

Python 是否有必要检查数据集中是否存在列,python,pandas,dataframe,Python,Pandas,Dataframe,列对于程序的执行至关重要,因此如果它不存在,您希望它死掉 df["column"] = df[["column1", "column2"]].apply(self._func, axis=1) if "vidcolumn1" in df.columns and "column2" in df.columns: df["column"] = df[["column1", "column2"]].apply(self._func, axis=1) else: raise ValueErro

列对于程序的执行至关重要,因此如果它不存在,您希望它死掉

df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
if "vidcolumn1" in df.columns and "column2" in df.columns:
  df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
else:
  raise ValueError("Column1 and Column2 are required for ...")
如果column1或column2不存在,这将抛出一个KeyError

df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
if "vidcolumn1" in df.columns and "column2" in df.columns:
  df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
else:
  raise ValueError("Column1 and Column2 are required for ...")
这似乎没有提供任何有用的东西,除非您想在else部分做一些事情,比如在日志的其他地方记录错误


因此,如果您的程序在缺少列的情况下没有回退,那么使用第二个选项有意义吗?

使用
尝试除外
语句:

try:
   df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
except KeyError:
    #error log
    print ('I have problem')

    print("Column1 and Column2 are required for ...")
#better code for test if exist all values of set in columns
s = set(['column1','column2'])
if s.issubset(set(df.columns)):
    df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
else:
    #error log
    print ('I have problem')
    raise ValueError("Column1 and Column2 are required for ...")
但在您的解决方案中,还应将代码添加到
else
语句中:

try:
   df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
except KeyError:
    #error log
    print ('I have problem')

    print("Column1 and Column2 are required for ...")
#better code for test if exist all values of set in columns
s = set(['column1','column2'])
if s.issubset(set(df.columns)):
    df["column"] = df[["column1", "column2"]].apply(self._func, axis=1)
else:
    #error log
    print ('I have problem')
    raise ValueError("Column1 and Column2 are required for ...")

实际上,我会使用try-except-catching
KeyError
s.@coldspeed如果pandas已经用一条有用的消息杀死程序,我不确定try/except是否有用:此列丢失。也许我遗漏了一点。@ClaudiuCreanga,我认为你想得太多了。谁说你必须使用第二种选择?您可以做很多事情:让错误发生,捕获它并做其他事情,捕获它并引发另一个错误,或者事先明确检查。你选择哪一个取决于你想发生什么。好的,但是你的尝试/例外不会终止程序。您必须手动提出ValueError。既然如此,为什么要用它呢?除了默认错误之外,它还提供了什么。@ClaudiuCreanga-hmmm,所以您想要第二个解决方案?或者更好,为什么你认为if else对你不好呢?我认为if else比一行难读,让pandas抛出一个错误。但事实上,我可能会选择try/catch,因为我也想把它记录在其他地方,也许这个专栏没有我想象的那么重要thought@ClaudiuCreanga-我现在感到困惑;)。还是不明白。@ClaudiuCreanga-hmm,这是一个很难回答的问题,因为在我看来是主观的。对我来说这很好,只是我改变了我的说法。这还取决于谁读代码——对于一个人来说,代码是可读的,而对于另一个人来说,代码不是可读的。因此,如果添加了一些注释,这是完全可以的,因为对于那些看起来难以阅读的人来说,它可以解释它。