replace in pandas

Posted under » Python Data Analysis on 26 May 2026

This is my way of having relational tables on pandas. For example instead of userid, I replace it with the real username.

A basic replace

>>> s = pd.Series([1, 2, 3, 4, 5])
>>> s.replace(1, 5)
0    5
1    2
2    3
3    4
4    5
dtype: int64

As usual, one replacement is not enough. You need a whole list.

df.replace([0, 1, 2, 3], [hanafi, brad, bush, travolta]) #list like
df.replace({0: 'hanafi', 1: 'brad', 2: 'bush', 3: 'travolta'}) #dict like

web security linux ubuntu Raspberry   git   javascript python django Laravel drupal php apache mysql  MongoDB AWS data  ML AI