[英]How to replace a pandas DataFrame column with lookup values from a dictionary?
Assume I have the following simple pandas DataFrame:假设我有以下简单的 pandas DataFrame:
df = pd.DataFrame({"id": [1, 2, 3, 4, 5],
"country": ["Netherlands", "Germany", "United_States", "England", "Canada"]})
and a dictionary with abbreviations for the values in the country
column:以及带有
country
/地区列中值缩写的字典:
abr = {"Netherlands": "NL",
"Germany": "GE",
"United_States": "US",
"England": "EN",
"Canada": "CA"
}
I want to change the values in the country
column of the DataFrame to the lookup values in the dictionary.我想将 DataFrame 的
country
列中的值更改为字典中的查找值。 The result would look like this:结果将如下所示:
id country
0 1 NE
1 2 GE
2 3 US
3 4 EN
4 5 CA
I tried to do it using我试着用
df["country"] = abr[df["country"]]
but that gives the following error:但这给出了以下错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
I understand why this error happens (the code tries to hash an object instead of the string value in the column), but is there a way to solve this?我了解为什么会发生此错误(代码尝试使用 hash 和 object 而不是列中的字符串值),但是有没有办法解决这个问题?
df["country"] = df["country"].map(abr)
print(df)
Prints:印刷:
id country
0 1 NL
1 2 GE
2 3 US
3 4 EN
4 5 CA
You can use pandas function replace()
especially thought for these scenarios.您可以使用 pandas function
replace()
特别考虑这些场景。 Careful not to confuse it with python's built-in .str.repace()
which doesn't take dictionaries.小心不要将它与不带字典的 python 内置
.str.repace()
混淆。
Try with:尝试:
df['country'] = df['country'].replace(abr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.