map unique values of 1 column to another pandas

Solutions on MaxInterview for map unique values of 1 column to another pandas by the best coders in the world

showing results for - "map unique values of 1 column to another pandas"
Nicola
02 Apr 2020
1cat_1 = [10, 11, 12]
2cat_2 = [25, 22, 30]
3cat_3 = [12, 14, 15]
4
5df1 = pd.DataFrame({'cat1':cat_1, 'cat2':cat_2, 'cat3':cat_3})
6
7all_cats = [10, 11, 12, 25, 22, 30, 15]
8cat_codes = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
9
10df2 = pd.DataFrame({'all_cats':all_cats, 'cat_codes':cat_codes})
11
12rename_dict = df2.set_index('all_cats').to_dict()['cat_codes']
13
14df1 = df1.replace(rename_dict)
15
similar questions