HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonCriticalCanonical

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
whosecolumnhowdropdataframepandasnanvaluerowscertain

Problem

I have this DataFrame and want only the records whose EPS column is not NaN:
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


...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


How do I do that?

Solution

Don't drop, just take the rows where EPS is not NA:

df = df[df['EPS'].notna()]

Context

Stack Overflow Q#13413590, score: 1650

Revisions (0)

No revisions yet.