Python Pandas Dataframe,删除“无”是任何列中的值的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45512763/
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
Python Pandas Dataframe, remove all rows where 'None' is the value in any column
提问by jlt199
I have a large dataframe. When it was created 'None' was used as the value where a number could not be calculated (instead of 'nan')
我有一个大数据框。创建时,“无”用作无法计算数字的值(而不是“nan”)
How can I delete all rows that have 'None' in any of it's columns? I though I could use df.dropna
and set the value of na
, but I can't seem to be able to.
如何删除任何列中包含“无”的所有行?我虽然可以使用df.dropna
和设置 的值na
,但我似乎不能。
Thanks
谢谢
I think this is a good representation of the dataframe:
我认为这是数据框的一个很好的表示:
temp = pd.DataFrame(data=[['str1','str2',2,3,5,6,76,8],['str3','str4',2,3,'None',6,76,8]])
回答by piRSquared
Setup
Borrowed @MaxU's df
设置
借用@MaxU'sdf
df = pd.DataFrame([
[1, 2, 3],
[4, None, 6],
[None, 7, 8],
[9, 10, 11]
], dtype=object)
Solution
You can just use pd.DataFrame.dropna
as is
解决方案
您可以pd.DataFrame.dropna
按原样使用
df.dropna()
0 1 2
0 1 2 3
3 9 10 11
Supposing you have None
strings like in this df
假设你有这样的None
字符串df
df = pd.DataFrame([
[1, 2, 3],
[4, 'None', 6],
['None', 7, 8],
[9, 10, 11]
], dtype=object)
Then combine dropna
with mask
然后结合dropna
使用mask
df.mask(df.eq('None')).dropna()
0 1 2
0 1 2 3
3 9 10 11
You can ensure that the entire dataframe is object
when you compare with.
您可以确保整个数据框object
与您进行比较时相同。
df.mask(df.astype(object).eq('None')).dropna()
0 1 2
0 1 2 3
3 9 10 11
回答by jlt199
Thanks for all your help. In the end I was able to get
感谢你的帮助。最后我能够得到
df = df.replace(to_replace='None', value=np.nan).dropna()
df = df.replace(to_replace='None', value=np.nan).dropna()
to work. I'm not sure why your suggestions didn't work for me.
上班。我不确定为什么你的建议对我不起作用。
回答by MaxU
UPDATE:
更新:
In [70]: temp[temp.astype(str).ne('None').all(1)]
Out[70]:
0 1 2 3 4 5 6 7
0 str1 str2 2 3 5 6 76 8
Old answer:
旧答案:
In [35]: x
Out[35]:
a b c
0 1 2 3
1 4 None 6
2 None 7 8
3 9 10 11
In [36]: x = x[~x.astype(str).eq('None').any(1)]
In [37]: x
Out[37]:
a b c
0 1 2 3
3 9 10 11
or bit nicer variant from @roganjosh:
In [47]: x = x[x.astype(str).ne('None').all(1)]
In [48]: x
Out[48]:
a b c
0 1 2 3
3 9 10 11