我有一个数据框,其中有一列包括列表和南。我想用 {} 替换所有空列表和 Nan。
我的数据是:
df['c'] = [[l1],[l2]], [], Nan
如果我使用
df['c'].apply(lambda x: {} if pd.isna(x) or len(x)==0 else x[0])
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
df['c'].apply(lambda x: {} if pd.isna(x).all() or len(x)==0 else x[0])
'bool' object has no attribute 'all'
我使用
创建了一个示例数据框df['c'] = [[4,5],[1,2,3]], [], np.nan
# Output
c
0 [[4, 5], [1, 2, 3]]
1 []
2 NaN
This should work -
df['c'] = df['c'].apply(lambda x: {} if x == [] or x is np.nan else x)