Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename Pandas column values

I have pandas dataframe like this

Column 1 Column 2
1 a
2 a
3 b
4 c
5 d

I want to name the column 2 as:

Column 1 Column 2
1 row1
2 row1
3 row2
4 row3
5 row4

I am trying the ways that are hard coded, like renaming each column, but in practice I have lots of rows so hard coding is not possible, is there any function or something python that can do the same task for me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Let’s try Series.factorize

df['Column2'] = (pd.Series(df['Column2'].factorize()[0])
                 .add(1).astype(str).radd('row'))
print(df)

   Column1 Column2
0        1    row1
1        2    row1
2        3    row2
3        4    row3
4        5    row4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading