Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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_List_Pandas_Dataframe - Fatal编程技术网

如何在Python中获取数据帧列中包含空列表的行

如何在Python中获取数据帧列中包含空列表的行,python,list,pandas,dataframe,Python,List,Pandas,Dataframe,我有这样一个数据框: 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'] 当列“B”包含空列表时,我想获取另一个包含行的数据帧。蟒蛇式的方法是什么 A B 0 a [] 非常感谢因为空列表将被解释为要以矢量化方式处理的集合

我有这样一个数据框:

    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']
当列“B”包含空列表时,我想获取另一个包含行的数据帧。蟒蛇式的方法是什么

    A    B
0   a   []

非常感谢

因为空列表将被解释为要以矢量化方式处理的集合,我看不到任何方法来测试它,而是深入到一个
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  []

空列表作为布尔值转换为False。使用
~
将其转换为True是一个简单的技巧。它很容易阅读,对我来说跑得很快。绝对比使用
len()快。


如果没有空字符串,可以使用
test[test['B'].str.len()==0]
是的,它似乎可以工作,我在不同的位置尝试了len(),但不适合我。谢谢
test[~test['B'].astype(bool)]

   A   B
0  a  []