create new column using dictionary padnas 1

create new column using dictionary padnas

U
111
112
112

d = {112: 'a', 111: 'b'}
df['B'] = df['A'].map(d)
df
Out[248]:
     A    B
0  111    a
1  112    b

Here is what the above code is Doing:
1. Create a dictionary with the mapping of values in column A to values in column B.
2. Use the map function to map the values in column A to the values in column B.
3. Assign the mapped values to column B.

Note that the map function can be used with a dictionary or a function.

Similar Posts