Using pandas:
df = pd.DataFrame({'n':['d','a','b','c','c','a','d','b'], 'v':[1,2,1,2,2,1,1,1]})
How can I rename the elements in df.n
, such that a
changes to x
, b
to y
, c
to w
and d
to z
, resulting in:
n v
0 z 1
1 x 2
2 y 1
3 w 2
...
You can pass a dictionary of replacement values into the Series replace method:
In [11]: df['n'].replace({'a': 'x', 'b': 'y', 'c': 'w', 'd': 'z'})
Out[11]:
0 z
1 x
2 y
3 w
4 w
5 x
6 z
7 y
Name: n, dtype: object
In [12]: df['n'] = df['n'].replace({'a': 'x', 'b': 'y', 'c': 'w', 'd': 'z'})