I have the following list:
list_1 = [{'28d_click': '2', 'action_type': 'comment', 'value': '2'},
{'28d_click': '1779',
'7d_view': '11144',
'action_type': 'offsite_conversion.custom.xx',
'value': '9425'},
{'28d_click': '122', 'action_type': 'landing_page_view', 'value': '122'},
{'28d_click': '21', 'action_type': 'like', 'value': '21'},
{'28d_click': '175', 'action_type': 'link_click', 'value': '175'},
{'28d_click': '1', 'action_type': 'post', 'value': '1'},
{'28d_click': '23', 'action_type': 'post_reaction', 'value': '23'},
{'28d_click': '222', 'action_type': 'page_engagement', 'value': '222'},
{'28d_click': '201', 'action_type': 'post_engagement', 'value': '201'},
{'28d_click': '1936',
'7d_view': '11171',
'action_type': 'offsite_conversion',
'value': '9607'}]
I have then used this list to create a pandas DataFrame: df = pd.DataFrame(list_1)
What I would like to do is use the action_type as columns and store the values in the rows beneath:
Below is just an example - please NOTE: some action_types (conversions) will need to have two fields (28d_click) and (7d_view) as separate columns. EG: Column1: Conversion_1 (28d_click) | Column2: Conversion_1 (7d_view)
landing_page_view link_click offsite_conversion.custom.xx (28d_click) offsite_conversion.custom.xx (7d_view).
122 175 7 16
What I have tried:
df = pd.DataFrame(list_1).T
Almost got the headings the way I wanted, but not quite with the 28d and 7d values
print([d['action_type'] for d in list_1 if 'action_type' in d])
print([d['value'] for d in list_1 if 'value' in d])
print([d['28d_click'] for d in list_1 if '28d_click' in d])
print([d['7d_view'] for d in list_1 if '7d_view' in d])
This creates individual lists, but then not entirely sure what to do with them
Is what I'm asking for possible?
I have another DataFrame which I want to join this with but wanted to get this right first.
Any help would be greatly appreciated.
Thanks,
Adrian