What is the best way to convert following pandas dataframe to a key value pair like below example:
dataframe:
col1 col2 col3 col4
0 a tom 90 Good
1 b jay 75 Not so good
2 c harsh 35 Bad
3 d ras 50 impresive
it is possible to get lists like this desired ouput:
[
{
'key':'col1',
'value':'a'
}
{
'key':'col2',
'value':'tom'
}
{
'key':'col3',
'value':90,
}
{
'key':'col4',
'value':'Good',
}
]
[
{
'key':'col1',
'value':'b'
}
{
'key':'col2',
'value':'jay'
}
{
'key':'col3',
'value':75,
}
{
'key':'col4',
'value':'Not so good',
}
]
... for every raw
Thank you in advance
CodePudding user response:
A more pythonic way:
>>> [[{'key': k, 'value': v} for v in l]
for _, k, l in zip(*df.to_dict('split').values())]
[[{'key': 'col1', 'value': 'a'},
{'key': 'col1', 'value': 'tom'},
{'key': 'col1', 'value': 90},
{'key': 'col1', 'value': 'Good'}],
[{'key': 'col2', 'value': 'b'},
{'key': 'col2', 'value': 'jay'},
{'key': 'col2', 'value': 75},
{'key': 'col2', 'value': 'Not so good'}],
[{'key': 'col3', 'value': 'c'},
{'key': 'col3', 'value': 'harsh'},
{'key': 'col3', 'value': 35},
{'key': 'col3', 'value': 'Bad'}],
[{'key': 'col4', 'value': 'd'},
{'key': 'col4', 'value': 'ras'},
{'key': 'col4', 'value': 50},
{'key': 'col4', 'value': 'impresive'}]]
Old answer
Use melt
reshape your dataframe then group by index and finally export each group to a list of dict:
>>> [df.to_dict('records') for _, df in df.melt(var_name='key', value_name='value', ignore_index=False).groupby(level=0)]
[[{'key': 'col1', 'value': 'a'},
{'key': 'col2', 'value': 'tom'},
{'key': 'col3', 'value': 90},
{'key': 'col4', 'value': 'Good'}],
[{'key': 'col1', 'value': 'b'},
{'key': 'col2', 'value': 'jay'},
{'key': 'col3', 'value': 75},
{'key': 'col4', 'value': 'Not so good'}],
[{'key': 'col1', 'value': 'c'},
{'key': 'col2', 'value': 'harsh'},
{'key': 'col3', 'value': 35},
{'key': 'col4', 'value': 'Bad'}],
[{'key': 'col1', 'value': 'd'},
{'key': 'col2', 'value': 'ras'},
{'key': 'col3', 'value': 50},
{'key': 'col4', 'value': 'impresive'}]]
CodePudding user response:
I am sure there are a more elegant way to do this but here a quick solution:
diktionary_list = []
end_list = []
for nummer, col in enumerate(frame):
for row in frame[col]:
dickt = {}
dickt["key"] = col
dickt["value"] = row
diktionary_list.append(dickt)
end_list.append(diktionary_list)