Python 使用 Pandas 替换数据框列中的特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40273067/
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 23:21:17  来源:igfitidea点击:

Replace specific values in a dataframe column using Pandas

pythonpandas

提问by ComplexData

I have a data frame df with a column called "Num_of_employees", which has values like 50-100, 200-500 etc. I see a problem with few values in my data. Wherever the employee number should be 1-10, the data has it as 10-Jan. Also, wherever the value should be 11-50, the data has it as Nov-50. How would I rectify this problem using pandas?

我有一个数据框 df,其中有一列名为“Num_of_employees”,它的值有 50-100、200-500 等。我发现数据中的值很少。员工编号应为 1-10 的地方,数据为 10-Jan。此外,无论值应该是 11-50,数据都将它作为 Nov-50。我将如何使用熊猫解决这个问题?

回答by maxymoo

A clean syntax for this kind of "find and replace" uses a dict, as

这种“查找和替换”的干净语法使用字典,如

df.Num_of_employees = df.Num_of_employees.replace({"10-Jan": "1-10",
                                                   "Nov-50": "11-50"})