[英]How to delete rows in a pandas dataframe?
我有這個熊貓數據框,它實際上是一個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
我想刪除ID
列中具有NaN
所有行,並對“索引虛數列”重新編制索引:
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
我知道可以做到以下幾點:
df = df['ID'].dropna()
要么
df[df.ID != np.nan]
要么
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''
要么
df[df.ID()]
要么:
df[df.ID != '']
接着:
df.reset_index(drop=True, inplace=True)
但是,它沒有刪除ID
的NaN
。 我正在獲取以前的數據框。
更新
在:
df['ID'].values
出:
array([ '....A lot of text....',
nan,
"A lot of text...",
"More text",
'text from the site',
nan,
"text from the site"], dtype=object)
嘗試df.dropna(axis = 1)
。
或者, df.dropna(axis = 0, subset = "ID")
看看是否有幫助。
嘗試這個
df = df[df.ID != 'nan']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.