1

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"}}
The Great
  • 7,215
  • 7
  • 40
  • 128

3 Answers3

3

You can use zip and dict comprehension:

col_indices = [1, 5]
t = {'thisdict': {"Ford": "brand", "Mustang": "model", 1964: "year"},
     'thatdict': {"jfsak": "af", "jhas": "asjf"}}

output = {i: v for i, v in zip(col_indices, t.values())}
print(output)
# {1: {'Ford': 'brand', 'Mustang': 'model', 1964: 'year'}, 5: {'jfsak': 'af', 'jhas': 'asjf'}}
j1-lee
  • 13,764
  • 3
  • 14
  • 26
  • 1
    I'm not sure this is what OP meant. As I read it they have a DataFrame with "thisdict" and "thatdict" in it and they want to replace the keys in `t` with "their corresponding column indices" from the DataFrame not necessarily hardcode them in. – 0x263A Feb 03 '22 at 04:34
  • 1
    @0x263A I am just using the given data as is. With no `df` at hand, what else could I do to reproduce the data? Of course one is free to replace the list with any other relevant iterator. – j1-lee Feb 03 '22 at 04:47
  • Understandable, added my answer to the question too – 0x263A Feb 03 '22 at 04:55
  • @0x263A Great! That would be helpful :) – j1-lee Feb 03 '22 at 04:55
1

Another option

df_list = df.columns.tolist()
t = {df_list.index(k): v for k, v in t.items()}

Btw, if you want to combine with your previous question here, you can try this

df_list = df.columns.tolist()
b = {df_list.index(tk): {v: k for k, v in tv.items()} for tk, tv in t.items()}
Thy
  • 468
  • 3
  • 5
1

To replace the keys in your dictionary t with their column index in the DataFrame you can lookup the index of the corresponding column in the DataFrame and assign it to a value in t like this:

import pandas
# Provided t
t = {'thisdict': {
    "Ford": "brand",
    "Mustang": "model",
    1964: "year"
},
    'thatdict': {
    "jfsak": "af",
    "jhas": "asjf"}
}

# Assumed df looks something like this
dct = {'thisdict': ['abc'],
       'thatdict': ['def']}
df = pandas.DataFrame(dct)

output = {df.columns.get_loc(name): dct for name, dct in t.items()}
print(output)

Output:

{0: {'Ford': 'brand', 'Mustang': 'model', 1964: 'year'}, 1: {'jfsak': 'af', 'jhas': 'asjf'}}

Note: This relies on all the keys in t existing in your DataFrame, but it would be relatively trivial to add checks if t is not one-to-one with the DataFrame.

0x263A
  • 1,807
  • 9
  • 22