create column with values mapped from another column python 1

create column with values mapped from another column python

In [55]:

import pandas as pd
equiv = {7001:1, 8001:2, 9001:3}
df = pd.DataFrame( {"A": [7001, 8001, 9001]} )
df["B"] = df["A"].map(equiv)
print(df)
      A  B
0  7001  1
1  8001  2
2  9001  3

[3 rows x 2 columns]

Here is what the above code is Doing:
1. Create a dictionary that maps the values in column A to the values in column B.
2. Create a DataFrame with a column A.
3. Create a new column B that maps the values in column A to the values in column B.

Similar Posts