Replace values in DataFrame column with a dictionary in Pandas
Replace values in column with a dictionary:
import pandas as pd df = pd.DataFrame({'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'] }, index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean', 'Christina', 'Cornelia']) print(df) dict = {"NY": 1, "TX": 2, "FL": 3, "AL": 4, "AK": 5} df1 = df.replace({"State": dict}) print("\n\n") print(df1)
C:\pandas>python example49.py State Jane NY Nick TX Aaron FL Penelope AL Dean AK Christina TX Cornelia TX State Jane 1 Nick 2 Aaron 3 Penelope 4 Dean 5 Christina 2 Cornelia 2 C:\pandas>
2018-11-18T11:51:21+05:30
2018-11-18T11:51:21+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution