Rename Pandas column values

Question:

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?

Asked By: MakM

||

Answers:

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
Answered By: Ynjxsjmh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.