如何删除在特定列中的值为NaN的Pandas DataFrame行
问题描述:
我有这个DataFrame
,并且只需要EPS
列不是NaN
的记录:
I have this DataFrame
and want only the records whose EPS
column is not NaN
:
>>> df
STK_ID EPS cash
STK_ID RPT_Date
601166 20111231 601166 NaN NaN
600036 20111231 600036 NaN 12
600016 20111231 600016 4.3 NaN
601009 20111231 601009 NaN NaN
601939 20111231 601939 2.5 NaN
000001 20111231 000001 NaN NaN
...即类似df.drop(....)
这样的结果帧:
...i.e. something like df.drop(....)
to get this resulting dataframe:
STK_ID EPS cash
STK_ID RPT_Date
600016 20111231 600016 4.3 NaN
601939 20111231 601939 2.5 NaN
我该怎么做?
答
不要删除,只要取EPS不是NA的行即可:
Don't drop, just take the rows where EPS is not NA:
df = df[df['EPS'].notna()]