sparrow sparrow - 1 year ago 191
Python Question

Drop null rows with dtype object from a DataFrame with Pandas

I have a DataFrame that looks like this:

Oper ST result
T2 9:24:09 NaN
T3 9:25:10 Fail
T4 9:25:36 Pass
T5 9:25:36 NaN

I want to drop the NaN rows from the 'result' column. All columns are dtype object:


Oper object
ST object
result object

If I print the values and dytpes to a list they appear in this format:

rlist = df['result'].tolist()
for r in rlist:

-> nan <class 'float'>

I tried these things unsuccessfully:




df = df[df['result'] != 'nan']

I also tried to find a way to convert the object to a string and then filter but could not find a way to do that either.

Answer Source

dropna does not work in-place: you have to assign the result to the dataframe itself or it will be lost:

 df = df.dropna(subset=['result'])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download