添加具有字典中映射值的新 Pandas 列 [英] Adding a new pandas column with mapped value from a dictionary

查看:32
本文介绍了添加具有字典中映射值的新 Pandas 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我希望结果如下:

      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆