Home > Mobile >  How to create a multi-dictionary call for a large dataframe
How to create a multi-dictionary call for a large dataframe

Time:12-17

I have created a simple dataframe for explanation purposes, however the original dataframe is much bigger with more than 100 columns and thousands rows. `

data = {'Name': ['James', 'Miriam', 'Josh', 'Peter'],
        'ID': [11,12,13,14],
        'Tester': [0,0,1,1],
       'Developer':[1,0,0,1],
       'Architect':[0,1,0,1],
       'Engineer':[1,1,0,0],
       'Cyber_security':[1,0,0,0],
       'IT_support':[0,1,0,0],
       'Business_analyst':[0,0,0,1]}

    Name    ID  Tester  Developer   Architect   Engineer    Cyber_security  IT_support  Business_analyst
0   James   11  0   1   0   1   1   0   0
1   Miriam  12  0   0   1   1   0   1   0
2   Josh    13  1   0   0   0   0   0   0
3   Peter   14  1   1   1   0   0   0   1

`

I am trying to find a single code that will automatically create separate dictionaries for every single column having always as a reference the 'Name' column. Also I would like to name those dictionaries with the same name of the column but adding "dict" or "list" to the name.

What I got at the moment is the following:

`
lstName = list ( df.Name )
dictTester = dict ( zip ( list( df.Name ), list ( df.Tester )))
dictDeveloper = dict ( zip ( list ( df.Name ), list ( df.Developer )))
dictArchitect = dict ( zip ( list ( df.Name ), list ( df.Architect )))
dictEngineer = dict ( zip ( list ( df.Name ), list ( df.Engineer )))
dictCyber_security = dict ( zip ( list ( df.Name ), list ( df.Cyber_security )))
dictIT_support= dict ( zip ( list ( df.Name ), list ( df.IT_support )))
dictBusiness_analyst = dict ( zip ( list ( df.Name ), list ( df.Business_analyst )))`

This works well, however if I want to do this with more than 100 columns, the codes will be a bit too long.

Can please anyone advice me how to get all in a single code and at the same time be able to name each dictionary as the codes above (dict columnname) so I will be able to call each dictionary independently later on to add them in constraints in a model.

CodePudding user response:

Maybe this would work?

In [41]: dff = df.to_dict(orient="list")

In [42]: for key, val in dff.items():
    ...:     if key == "Name":
    ...:         continue
    ...:     d ={key: {name:value for name, value in zip(dff["Name"], val)}}
    ...:     print(d)
    ...:
{'ID': {'James': 11, 'Miriam': 12, 'Josh': 13, 'Peter': 14}}
{'Tester': {'James': 0, 'Miriam': 0, 'Josh': 1, 'Peter': 1}}
{'Developer': {'James': 1, 'Miriam': 0, 'Josh': 0, 'Peter': 1}}
{'Architect': {'James': 0, 'Miriam': 1, 'Josh': 0, 'Peter': 1}}
{'Engineer': {'James': 1, 'Miriam': 1, 'Josh': 0, 'Peter': 0}}
{'Cyber_security': {'James': 1, 'Miriam': 0, 'Josh': 0, 'Peter': 0}}
{'IT_support': {'James': 0, 'Miriam': 1, 'Josh': 0, 'Peter': 0}}
{'Business_analyst': {'James': 0, 'Miriam': 0, 'Josh': 0, 'Peter': 1}}

Not sure if that is what you want?

Updated answer that defines variables:

In [139]:
     ...: for key, val in dff.items():
     ...:     if key == "Id":
     ...:         continue
     ...:     variable_name = f"dict{key}"
     ...:     value = {name:value for name, value in zip(dff["Name"], val)}
     ...:     # This was wrong.
     ...:     # exec("variable_name = value")
     ...:     # Fix
     ...:     exec(f"{variable_name} = value")

In [140]: dictDeveloper
Out[140]: {'James': 1, 'Miriam': 0, 'Josh': 0, 'Peter': 1}
  • Related