Home > Mobile >  column of lists to lists and dictionaries
column of lists to lists and dictionaries

Time:12-21

having two columns in a dataframe, I want to use the first column values as keys for the other as a dictionary

suppose the df is as follows

Variable Value Value Distribution
1 First Color ['Black', 'Blue', 'Green', 'Red', 'Purple'] [0.3, 0.25, 0.2, 0.15, 0.1]
5 Second Color ['Deep Blue', 'Teal', 'Green', 'Purple ', 'Red... [0.5, 0.25, 0.15, 0.25, 0.25, 0.15, 0.1]
6 Third Color ['Red', 'Orange', 'Yellow', 'Green', 'Blue', '... [1.0, 0.0, 0.0, 0.0, 0.0, 0.0]

so suppose I want to create a dic like

{'First Color':{'Black':0.3,'Blue':0.25,'Green':0.2,'Red':0.15,'Purple':0.1}

so I tried the following

dict(zip(df['Value'],df['Value Distribution']))

to zip both the second and the third column-values to a dictionary but instead of that it created this dictionary

"['Black', 'Blue', 'Green', 'Red', 'Purple']":"[0.3, 0.25, 0.2, 0.15, 0.1]" reading the lists as strings

CodePudding user response:

Try with explode and groupby:

df = df.explode(["Value", "Value Distribution"])
>>> df.groupby("Variable").apply(lambda x: dict(zip(x["Value"],x["Value Distribution"]))).to_dict()
{'First Color': {'Black': 0.3,
  'Blue': 0.25,
  'Green': 0.2,
  'Red': 0.15,
  'Purple': 0.1},
 'Second Color': {'Deep Blue': 0.5,
  'Teal': 0.25,
  'Green': 0.15,
  'Purple ': 0.25,
  'Red': 0.25},
 'Third Color': {'Red': 1.0,
  'Orange': 0.0,
  'Yellow': 0.0,
  'Green': 0.0,
  'Blue': 0.0}}

CodePudding user response:

This is probably easiest to lay out with iterrows:

df = pd.DataFrame(
    data = [
        ['First colour', ['Black', 'Blue', 'Green', 'Red', 'Purple'], [0.3, 0.25, 0.2, 0.15, 0.1]],
        ['Second Color', ['Red', 'Orange', 'Yellow', 'Green', 'Blue'], [0.5, 0.25, 0.15, 0.25, 0.25, 0.15, 0.1]]
    ],
    columns=['Variable', 'Value', 'Value Distribution']
)

dict_result = {}
for index, row in df.iterrows():
    dict_result[row['Variable']] = dict(zip(row['Value'],row['Value Distribution']))

CodePudding user response:

dct = df.set_index('Variable').apply(lambda x: dict(zip(x["Value"], x["Value Distribution"])), axis=1).to_dict()

Output:

>>> dct
{'First Color': {'Black': 0.3,
  'Blue': 0.25,
  'Green': 0.2,
  'Red': 0.15,
  'Purple': 0.1}}
  • Related