I have a list and dict like as shown below
col_indices = [df.columns.tolist().index(col) for col in cat_cols]
print(col_indices) #returns [1,5]
t = {'thisdict':{
"Ford":"brand",
"Mustang":"model",
1964:"year"
},
'thatdict':{
"jfsak":"af",
"jhas":"asjf"}}
Basically, I would like to replace dict keys with their corresponding column indices.
For ex: column index 1 belongs to thisdict
and column index 5 belongs to thatdict
.
I was trying something like below but doesn't work.
key_map_dict = {'1':'thisdict','5':'thatdict'}
d = {(key_map_dict[k] if k in key_map_dict else k):v for (k,v) in t.items() }
Instead of me manually defining key_map_dict
. Is there anyway to find the matching column names and get the index position and do the replacement in dicts automatically? I cannot do this for big data frame of million rows and 200 columns.
I expect my output to be like as shown below
{1:{
"Ford":"brand",
"Mustang":"model",
1964:"year"
},
5:{
"jfsak":"af",
"jhas":"asjf"}}