如何从列中删除空列表? [重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

我有一个看起来像这样的df

list
[]
[8,8]
[0,9]
[]

此列的qtype为object。如何从列中删除[]并替换为np.nan?

我尝试过字符串替换,但括号不是

df.list = df.list.replace('"[]"','')
python-3.x string pandas
2个回答
3
投票

使用astype(bool)

df=df[df['list'].astype(bool)]

0
投票

你也可以用这个:

In [432]: df[df.astype(str)['list'] != '[]']
Out[432]: 
    list
1  [8,8]
2  [0,9]
© www.soinside.com 2019 - 2024. All rights reserved.