Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename column values using pandas DataFrame

in one of the columns in my dataframe I have five values:

1,G,2,3,4

How to make it change the name of all "G" to 1

I tried:

df = df['col_name'].replace({'G': 1})

I also tried:

df = df['col_name'].replace('G',1)

"G" is in fact 1 (I do not know why there is a mixed naming)

Edit:

works correctly with:

df['col_name'] = df['col_name'].replace({'G': 1})
like image 251
tbone Avatar asked Jul 23 '19 13:07

tbone


1 Answers

If I am understanding your question correctly, you are trying to change the values in a column and not the column name itself.

Given you have mixed data type there, I assume that column is of type object and thus the number is read as string.

df['col_name'] = df['col_name'].str.replace('G', '1')
like image 167
addicted Avatar answered Sep 27 '22 17:09

addicted