使用 Pandas 替换数据框列中的特定值 [英] Replace specific values in a dataframe column using Pandas

查看:77
本文介绍了使用 Pandas 替换数据框列中的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

这种查找和替换"的简洁语法使用字典,如

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"})

这篇关于使用 Pandas 替换数据框列中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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