Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming values in pandas

Tags:

python

pandas

I do have a DataFrame like this:

col1  col 2
abc   sure
def   yes
ghi   no
jkl   no 
mno   sure
pqr   yes
stu   sure

My intention is to rename "sure" and "yes" into "confirm", so that the DataFrame looks like:

col1  col 2
abc   confirm
def   confirm
ghi   no
jkl   no 
mno   confirm
pqr   confirm
stu   confirm

How to do this :)?

like image 533
Christopher Avatar asked Oct 20 '15 10:10

Christopher


People also ask

How do I rename a value in a column in Pandas?

We can use pandas DataFrame rename() function to rename columns and indexes.

How do I rename a variable in Pandas?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.


1 Answers

You can just:

df = df.replace(['yes','sure'],'confirm')
like image 114
Fabio Lamanna Avatar answered Sep 28 '22 04:09

Fabio Lamanna