Python 从字典中添加具有映射值的新 Pandas 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24216425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:14:30  来源:igfitidea点击:

Adding a new pandas column with mapped value from a dictionary

pythonpandas

提问by Rick Donnelly

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:

我正在尝试在 Pandas 中做一些应该非常简单的事情,但似乎什么都不是。我正在尝试向现有的 Pandas 数据框添加一列,该列是基于另一个(现有)列的映射值。这是一个小测试用例:

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

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:

相反,我收到一条错误消息,告诉我 equiv 不是可调用函数。很公平,它是一本字典,但即使我将它包装在一个函数中,我仍然感到沮丧。所以我尝试使用一个似乎可以与其他操作一起使用的 map 函数,但它也被字典的使用打败了:

df["B"] = df["A"].map(lambda x:equiv[x])

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.

在这种情况下,我只得到 KeyError: 8001。我已经阅读了文档和以前的帖子,但还没有遇到任何建议如何将字典与 Pandas 数据框混合的内容。任何建议将不胜感激。

回答by CT Zhu

The right way of doing it will be df["B"] = df["A"].map(equiv).

正确的做法是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]