daiyue
daiyue

Reputation: 7458

pandas removes rows with nan in df

I have a df that contains nan,

A  
nan  
nan
nan
nan
2017
2018

I tried to remove all the nan rows in df,

df = df.loc[df['A'].notnull()]

but df still contains those nan values for column 'A' after the above code. The dtype of 'A' is object.

I am wondering how to fix it. The thing is I need to define multiple conditions to filter the df, and df['A'].notnull() is one of them. Don't know why it doesn't work.

Upvotes: 0

Views: 97

Answers (1)

jpp
jpp

Reputation: 164803

Please provide a reproducible example. As such this works:

import pandas as pd
import numpy as np

df = pd.DataFrame([[np.nan], [np.nan], [2017], [2018]], columns=['A'])
df = df[df['A'].notnull()]

df2 = pd.DataFrame([['nan'], ['nan'], [2017], [2018]], columns=['A'])
df2 = df2.replace('nan', np.nan)
df2 = df2[df2['A'].notnull()]

# output [df or df2]
# A
# 2017.0 
# 2018.0 

Upvotes: 1

Related Questions