1import pandas as pd
2import numpy as np
3data = {'Name':['Tom', 'Tom', 'Jack', 'Terry'], 'Age':[20, 21, 19, 18]}
4df = pd.DataFrame(data)
5
6nth = ['First', 'Second', 'Third', 'Fourth']
7
8def prefix(d):
9 n = len(d)
10 if n > 1:
11 return d.radd([nth[i] for i in range(n)])
12 else:
13 return d
14
15df.assign(Name=df.groupby('Name').Name.transform(prefix))
16
17 Name Age
180 FirstTom 20
191 SecondTom 21
202 Jack 19
213 Terry 18
22
23