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?
>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