【发布时间】:2014-12-24 05:16:19
【问题描述】:
我有一个数据框(在 Python 2.7 中,pandas 0.15.0):
df=
A B C
0 NaN 11 NaN
1 two NaN ['foo', 'bar']
2 three 33 NaN
我想对特定列中不包含 NULL 值的行应用一个简单的函数。我的功能尽可能简单:
def my_func(row):
print row
我的申请代码如下:
df[['A','B']].apply(lambda x: my_func(x) if(pd.notnull(x[0])) else x, axis = 1)
完美运行。如果我想检查 'B' 列的 NULL 值,pd.notnull() 也可以完美运行。但是,如果我选择包含列表对象的列“C”:
df[['A','C']].apply(lambda x: my_func(x) if(pd.notnull(x[1])) else x, axis = 1)
然后我收到以下错误消息:ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', u'occurred at index 1')
有人知道为什么pd.notnull() 只适用于整数和字符串列而不适用于“列表列”吗?
还有更好的方法来检查“C”列中的 NULL 值而不是这个:
df[['A','C']].apply(lambda x: my_func(x) if(str(x[1]) != 'nan') else x, axis = 1)
谢谢!
【问题讨论】:
标签: python list pandas null apply