Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace specific values in a dataframe column using Pandas

Tags:

python

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?

like image 259
ComplexData Avatar asked Oct 26 '16 22:10

ComplexData


People also ask

How do I change a specific value in pandas?

Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.

How do you replace values in a column?

Select a column with a text data type. Select Home or Transform > Replace Value.

How do you conditionally replace values in pandas?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.


1 Answers

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"})
like image 87
maxymoo Avatar answered Oct 20 '22 00:10

maxymoo