我知道如何使用字典替换列值,但是我想将字典中没有的所有值转换为NaN或其他值。我明白了:
Dictionary is:
{'apple': 1, 'peach': 6, 'watermelon': 4, 'grapes': 5, 'orange': 2,
'banana': 3}
DataFrame is:
fruit_tag
apple
orange
banana
watermelon
red
blue
I use:
df["fruit_tag"].replace(dict, inplace=True)
print(df)
I get:
fruit_tag
1
2
3
4
red
blue
What I want to get:
fruit_tag
1
2
3
4
NaN
NaN
使用^{} :
相关问题 更多 >
编程相关推荐