python: pandas 应用函数:InvalidIndexError

python: pandas apply function: InvalidIndexError

我在我的数据框 my_df 上使用 apply,如下所示:

my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'] )

我要:

  if x['column_B'] = None -> return 'hello'
  if x['column_B'] != None -> return x['column_B']

然后我得到了以下错误:

<ipython-input-31-aa087c9a635e> in <lambda>(x)
----> 1 my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'] )

/usr/local/lib/python3.4/dist-packages/pandas/core/series.py in __getitem__(self, key)
    599         key = com._apply_if_callable(key, self)
    600         try:
--> 601             result = self.index.get_value(self, key)
    602 
    603             if not is_scalar(result):

/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_value(self, series, key)
   2187             # python 3
   2188             if is_scalar(key):  # pragma: no cover
-> 2189                 raise IndexError(key)
   2190             raise InvalidIndexError(key)
   2191 

IndexError: ('column_B', 'occurred at index column_A')

有谁知道我在这里做错了什么?

您需要应用指定 axis=1 才能将其应用到每一行,而不是每一列。请参阅 DataFrame.apply:

上的文档
axis : {0 or 'index', 1 or 'columns'}, default 0

* 0 or 'index': apply function to each column
* 1 or 'columns': apply function to each row

在您当前的调用中,当它真正使用对应于 column_A.

的 pd.Series 时,它找不到 x['column_B']

因此,如果您使用以下内容,它将起作用。

my_df['column_C'] = my_df.apply(lambda x : 'hello' 
                                if x['column_B'] is None
                                else x['column_B'], axis=1)

注意:如上评论所述,DataFrame.fillna 更适合此任务。