Sap*_*ing 4 python list dataframe pandas
我有一个这样的数据框:
test = pd.DataFrame(columns=['A','B'])
test.loc[0,'A'] = 'a'
test.loc[0,'B'] = []
test.loc[1,'A'] = 'b'
test.loc[1,'B'] = ['a','b']
Run Code Online (Sandbox Code Playgroud)
当“B”列包含空列表时,我想获取另一个包含行的数据框。这样做的pythonic方式是什么?
A B
0 a []
Run Code Online (Sandbox Code Playgroud)
非常感谢
由于空列表将被解释为以矢量化方式处理的集合,我看不到任何方法来测试它,只能深入apply调用:
test.B.apply(lambda c: c==[])
Out[71]:
0 True
1 False
Name: B, dtype: bool
test[test.B.apply(lambda c: c==[])]
Out[72]:
A B
0 a []
Run Code Online (Sandbox Code Playgroud)