Skip to content
Advertisement

Got Nan while mapping the values in dataframe

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

Got NaN instead of man&woman

What is wrong?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement