rename elements in a column of a data frame using pandas rename elements in a column of a data frame using pandas pandas pandas

rename elements in a column of a data frame using pandas


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    z1    x2    y3    w4    w5    x6    z7    yName: n, dtype: objectIn [12]: df['n'] = df['n'].replace({'a': 'x', 'b': 'y', 'c': 'w', 'd': 'z'})


You can also use the below one:

df['n'].replace(['a', 'b', 'c', 'd'], ['x', 'y', 'w', 'z'])

Replace all the a with x, b with y, c with w, and d with z. Note: if you pass two lists they both much be the same length