Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - replacing column values

Tags:

python

pandas

I know there are a number of topics on this question, but none of the methods worked for me so I'm posting about my specific situation

I have a dataframe that looks like this:

data = pd.DataFrame([[1,0],[0,1],[1,0],[0,1]], columns=["sex", "split"]) data['sex'].replace(0, 'Female') data['sex'].replace(1, 'Male') data 

What I want to do is replace all 0's in the sex column with 'Female', and all 1's with 'Male', but the values within the dataframe don't seem to change when I use the code above

Am I using replace() incorrectly? Or is there a better way to do conditional replacement of values?

like image 963
Simon Avatar asked Aug 08 '15 01:08

Simon


People also ask

How do I replace values in multiple columns in Pandas?

Pandas replace multiple values in column replace. By using DataFrame. replace() method we will replace multiple values with multiple new strings or text for an individual DataFrame column. This method searches the entire Pandas DataFrame and replaces every specified value.

How do I replace an entire column in Pandas?

In order to replace a value in Pandas DataFrame, use the replace() method with the column the from and to values.

How do you replace a column with another column value in Python?

DataFrame. replace() function is used to replace values in column (one value with another value on all columns). This method takes to_replace, value, inplace, limit, regex and method as parameters and returns a new DataFrame. When inplace=True is used, it replaces on existing DataFrame object and returns None value.


1 Answers

Yes, you are using it incorrectly, Series.replace() is not inplace operation by default, it returns the replaced dataframe/series, you need to assign it back to your dataFrame/Series for its effect to occur. Or if you need to do it inplace, you need to specify the inplace keyword argument as True Example -

data['sex'].replace(0, 'Female',inplace=True) data['sex'].replace(1, 'Male',inplace=True) 

Also, you can combine the above into a single replace function call by using list for both to_replace argument as well as value argument , Example -

data['sex'].replace([0,1],['Female','Male'],inplace=True) 

Example/Demo -

In [10]: data = pd.DataFrame([[1,0],[0,1],[1,0],[0,1]], columns=["sex", "split"])  In [11]: data['sex'].replace([0,1],['Female','Male'],inplace=True)  In [12]: data Out[12]:       sex  split 0    Male      0 1  Female      1 2    Male      0 3  Female      1 

You can also use a dictionary, Example -

In [15]: data = pd.DataFrame([[1,0],[0,1],[1,0],[0,1]], columns=["sex", "split"])  In [16]: data['sex'].replace({0:'Female',1:'Male'},inplace=True)  In [17]: data Out[17]:       sex  split 0    Male      0 1  Female      1 2    Male      0 3  Female      1 
like image 127
Anand S Kumar Avatar answered Sep 17 '22 23:09

Anand S Kumar