Home > Software engineering >  I want to replace NaN value in one column, but my other column is condition
I want to replace NaN value in one column, but my other column is condition

Time:06-02

I have a problem with replacing NaN values in my DataFrame

I am using Data from Kaggle and I want to do the project. I found correlation between two columns. I want to replace my NaN value in column 'VIP' to be 0, only if in the column 'HomePlanet' is value Earth. How to do that?

I hope you understand my question, thx.

CodePudding user response:

df.loc[ (df.HomePlanet == 'Earth') & (df.VIP == NaN), 'VIP'] = 0

CodePudding user response:

You can switch to numpy very easily :

VIP = df['VIP'].to_numpy()
HP = df['HomePlanet'].to_numpy()
VIP[HP == 'Earth'] = 0
df['VIP'] = VIP

CodePudding user response:

You can also do this:

df.loc[df.HomePlanet == "Earth", "VIP"] = df.fillna(0)
  • Related