Home > Back-end >  Rename string values automatically in pandas
Rename string values automatically in pandas

Time:03-18

I have a dataframe like this:

Var Name Val
a 11
a 1
a 2
b 3
b 4

I would like to add a column with updated and enumerated "Var Name", something like this

Var Name Val Var Name
a 11 a1
a 1 a2
a 2 a3
b 3 b1
b 4 b2

My idea is to enumerate the Var Name (1,2,3...) till it "recognizes" a new Var Name and start enumerating again from 1.

CodePudding user response:

groupby cumcount is what you need here:

df['Var Name2'] = df['Var Name']   df.groupby('Var Name').cumcount().add(1).astype(str)

Output:

>>> df
  Var Name  Val Var Name2
0        a   11        a1
1        a    1        a2
2        a    2        a3
3        b    3        b1
4        b    4        b2

To add extra characters, such as an underscore:

df['Var Name2'] = df['Var Name']   '_'   df.groupby('Var Name').cumcount().add(1).astype(str)
  • Related