Home > Software engineering >  Got Nan while mapping the values in dataframe
Got Nan while mapping the values in dataframe

Time:04-30

df['gender'] = df['gender'].map({"2": "man", "1": "woman"})

Got NaN instead of man&woman

What is wrong?

CodePudding user response:

I think the type of gender is int, so this would fix your problem:

import pandas as pd
df=pd.DataFrame()
df["gender"]=[1,2,1,2,2,1]
df['gender'] = df['gender'].map({2: "man", 1: "woman"})
print(df)

The output:

  gender
0  woman
1    man
2  woman
3    man
4    man
5  woman
  • Related