从字典添加具有映射值的新pandas列 [英] Adding a new pandas column with mapped value from a dictionary
问题描述
我正在尝试做一些在熊猫中应该非常简单的事情,但是似乎没什么.我正在尝试向现有的熊猫数据框添加一列,该数据框是基于另一个(现有)列的映射值.这是一个小测试用例:
I'm trying do something that should be really simple in pandas, but it seems anything but. I'm trying to add a column to an existing pandas dataframe that is a mapped value based on another (existing) column. Here is a small test case:
import pandas as pd
equiv = {7001:1, 8001:2, 9001:3}
df = pd.DataFrame( {"A": [7001, 8001, 9001]} )
df["B"] = equiv(df["A"])
print(df)
我希望能得到以下结果:
I was hoping the following would result:
A B
0 7001 1
1 8001 2
2 9001 3
相反,我收到一条错误消息,告诉我equiv不是可调用函数.公平地说,它是一个字典,但是即使将其包装在一个函数中,我仍然会感到沮丧.因此,我尝试使用似乎可以与其他操作配合使用的map函数,但也由于使用字典而失败:
Instead, I get an error telling me that equiv is not a callable function. Fair enough, it's a dictionary, but even if I wrap it in a function I still get frustration. So I tried to use a map function that seems to work with other operations, but it also is defeated by use of a dictionary:
df["B"] = df["A"].map(lambda x:equiv[x])
在这种情况下,我只会得到KeyError:8001.我已经阅读了文档和以前的文章,但是还没有发现任何建议如何将字典与pandas数据框混合使用的信息.任何建议将不胜感激.
In this case I just get KeyError: 8001. I've read through documentation and previous posts, but have yet to come across anything that suggests how to mix dictionaries with pandas dataframes. Any suggestions would be greatly appreciated.
推荐答案
正确的方法是df["B"] = df["A"].map(equiv)
.
In [55]:
import pandas as pd
equiv = {7001:1, 8001:2, 9001:3}
df = pd.DataFrame( {"A": [7001, 8001, 9001]} )
df["B"] = df["A"].map(equiv)
print(df)
A B
0 7001 1
1 8001 2
2 9001 3
[3 rows x 2 columns]
考虑以下示例,它将很好地处理密钥不存在的情况:
And it will handle the situation when the key does not exist very nicely, considering the following example:
In [56]:
import pandas as pd
equiv = {7001:1, 8001:2, 9001:3}
df = pd.DataFrame( {"A": [7001, 8001, 9001, 10000]} )
df["B"] = df["A"].map(equiv)
print(df)
A B
0 7001 1
1 8001 2
2 9001 3
3 10000 NaN
[4 rows x 2 columns]
这篇关于从字典添加具有映射值的新pandas列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!