I would make use of categorical dtype:
In [97]: x['ID'] = x.name.astype('category').cat.rename_categories(range(1, x.name.nunique()+1))
In [98]: x
Out[98]:
name v1 v2 v3 ID
0 Aaron 3 5 7 1
1 Aaron 3 6 9 1
2 Aaron 3 6 10 1
3 Brave 4 6 0 2
4 Brave 3 6 1 2
if you need string IDs instead of numerical ones, you can use:
x.name.astype('category').cat.rename_categories([str(x) for x in range(1,x.name.nunique()+1)])
or, as @MedAli has mentioned in his answer, using factorize() method - demo:
In [141]: x['cat'] = pd.Categorical((pd.factorize(x.name)[0] + 1).astype(str))
In [142]: x
Out[142]:
name v1 v2 v3 ID cat
0 Aaron 3 5 7 1 1
1 Aaron 3 6 9 1 1
2 Aaron 3 6 10 1 1
3 Brave 4 6 0 2 2
4 Brave 3 6 1 2 2
In [143]: x.dtypes
Out[143]:
name object
v1 int64
v2 int64
v3 int64
ID category
cat category
dtype: object
In [144]: x['cat'].cat.categories
Out[144]: Index(['1', '2'], dtype='object')
or having categories as integer numbers:
In [154]: x['cat'] = pd.Categorical((pd.factorize(x.name)[0] + 1))
In [155]: x
Out[155]:
name v1 v2 v3 ID cat
0 Aaron 3 5 7 1 1
1 Aaron 3 6 9 1 1
2 Aaron 3 6 10 1 1
3 Brave 4 6 0 2 2
4 Brave 3 6 1 2 2
In [156]: x['cat'].cat.categories
Out[156]: Int64Index([1, 2], dtype='int64')
explanation:
In [99]: x.name.astype('category')
Out[99]:
0 Aaron
1 Aaron
2 Aaron
3 Brave
4 Brave
Name: name, dtype: category
Categories (2, object): [Aaron, Brave]
In [100]: x.name.astype('category').cat.categories
Out[100]: Index(['Aaron', 'Brave'], dtype='object')
In [101]: x.name.astype('category').cat.rename_categories([1,2])
Out[101]:
0 1
1 1
2 1
3 2
4 2
dtype: category
Categories (2, int64): [1, 2]
explanation for the factorize() method:
In [157]: (pd.factorize(x.name)[0] + 1)
Out[157]: array([1, 1, 1, 2, 2])
In [158]: pd.Categorical((pd.factorize(x.name)[0] + 1))
Out[158]:
[1, 1, 1, 2, 2]
Categories (2, int64): [1, 2]