在映射数据框中的值时得到 Nan

Got Nan while mapping the values in dataframe

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

得到NaN而不是男人&女人

怎么了?

我认为性别的类型是整数,所以这可以解决您的问题:

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)

输出:

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