pandas 如何删除熊猫数据框中的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40872090/
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
How to delete rows in a pandas dataframe?
提问by student
I have this pandas dataframe which is actually a excel spreadsheet:
我有这个 pandas 数据框,它实际上是一个 excel 电子表格:
Unnamed: 0 Date Num Company Link ID
0 NaN 1990-11-15 131231 apple... http://www.example.com/201611141492/xellia... 290834
1 NaN 1990-10-22 1231 microsoft http://www.example.com/news/arnsno... NaN
2 NaN 2011-10-20 123 apple http://www.example.com/ator... 209384
3 NaN 2013-10-27 123 apple... http://example.com/sections/th-shots/2016/... 098
4 NaN 1990-10-26 123 google http://www.example.net/business/Drugmak... 098098
5 NaN 1990-10-18 1231 google... http://example.com/news/va-rece... NaN
6 NaN 2011-04-26 546 amazon... http://www.example.com/news/home/20160425... 9809
I would like to remove all the rows that have NaN
in the ID
column and reindex the "index imaginary column":
我想删除所有具有行NaN
中ID
列和重新索引“指数虚列”:
Unnamed: 0 Date Num Company Link ID
0 NaN 1990-11-15 131231 apple... http://www.example.com/201611141492/xellia... 290834
1 NaN 2011-10-20 123 apple http://www.example.com/ator... 209384
2 NaN 2013-10-27 123 apple... http://example.com/sections/th-shots/2016/... 098
3 NaN 1990-10-26 123 google http://www.example.net/business/Drugmak... 098098
4 NaN 2011-04-26 546 amazon... http://www.example.com/news/home/20160425... 9809
I know that this can be done as follows:
我知道这可以按如下方式完成:
df = df['ID'].dropna()
Or
或者
df[df.ID != np.nan]
Or
或者
df = df[np.isfinite(df['ID'])]
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Or
或者
df[df.ID()]
Or:
或者:
df[df.ID != '']
And then:
进而:
df.reset_index(drop=True, inplace=True)
However, It didnt removed the NaN
in ID
. I am getting the former dataframe.
但是,它没有删除NaN
in ID
。我正在获取以前的数据框。
UPDATE
更新
In:
在:
df['ID'].values
Out:
出去:
array([ '....A lot of text....',
nan,
"A lot of text...",
"More text",
'text from the site',
nan,
"text from the site"], dtype=object)
回答by Mainul Islam
Try df.dropna(axis = 1)
.
试试df.dropna(axis = 1)
。
Or, df.dropna(axis = 0, subset = "ID")
See if it helps.
或者,df.dropna(axis = 0, subset = "ID")
看看它是否有帮助。
回答by ??????
try this
尝试这个
df = df[df.ID != 'nan']