sparrow sparrow - 2 months ago 11
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:

df.dtypes

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:
print(r,type(r))

-> nan <class 'float'>


I tried these things unsuccessfully:

dropna:

df.dropna(subset=['result'])


filtering:

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

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'])
Comments