从 Pandas 数据框中删除带有空列表的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34162625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove rows with empty lists from pandas data frame
提问by Ben Price
I have a data frame with some columns with empty lists and others with lists of strings:
我有一个数据框,其中一些列带有空列表,而另一些列带有字符串列表:
donation_orgs donation_context
0 [] []
1 [the research of Dr. ...] [In lieu of flowers , memorial donations ...]
I'm trying to return a data set without any of the rows where there are empty lists.
我正在尝试返回一个没有任何空列表行的数据集。
I've tried just checking for null values:
我试过只检查空值:
dfnotnull = df[df.donation_orgs != []]
dfnotnull
and
和
dfnotnull = df[df.notnull().any(axis=1)]
pd.options.display.max_rows=500
dfnotnull
And I've tried looping through and checking for values that exist, but I think the lists aren't returning Null or None like I thought they would:
我已经尝试遍历并检查存在的值,但我认为列表不会像我想象的那样返回 Null 或 None :
dfnotnull = pd.DataFrame(columns=('donation_orgs', 'donation_context'))
for i in range(0,len(df)):
if df['donation_orgs'].iloc(i):
dfnotnull.loc[i] = df.iloc[i]
All three of the above methods simply return every row in the original data frame.=
以上三种方法都简单地返回原始数据框中的每一行。=
回答by Victor
To avoid converting to str
and actually use the list
s, you can do this:
为避免转换为str
并实际使用list
s,您可以这样做:
df[df['donation_orgs'].map(lambda d: len(d)) > 0]
It maps the donation_orgs
column to the length of the listsof each row and keeps only the ones that have at least one element, filtering out empty lists.
它将donation_orgs
列映射到每一行的列表的长度,并只保留至少具有一个 element 的那些,过滤掉空列表。
It returns
它返回
Out[1]:
donation_context donation_orgs
1 [In lieu of flowers , memorial donations] [the research of Dr.]
as expected.
正如预期的那样。
回答by Woody Pride
You could try slicing as though the data frame were strings instead of lists:
您可以尝试切片,就好像数据框是字符串而不是列表:
import pandas as pd
df = pd.DataFrame({
'donation_orgs' : [[], ['the research of Dr.']],
'donation_context': [[], ['In lieu of flowers , memorial donations']]})
df[df.astype(str)['donation_orgs'] != '[]']
Out[9]:
donation_context donation_orgs
1 [In lieu of flowers , memorial donations] [the research of Dr.]
回答by Amir Imani
You can use the following one-liner:
您可以使用以下单行:
df[(df['donation_orgs'].str.len() != 0) | (df['donation_context'].str.len() != 0)]
回答by Mark
Assuming that you read data from a CSV, the other possible solution could be this:
假设您从 CSV 读取数据,另一种可能的解决方案可能是:
import pandas as pd
df = pd.read_csv('data.csv', na_filter=True, na_values='[]')
df.dropna()
na_filter
defines additional string to recognize as NaN. I tested this on pandas-0.24.2
.
na_filter
定义附加字符串以识别为 NaN。我在pandas-0.24.2
.