I’m getting data with Facebook Insights API and there are nested columns in the data I get. I tried separating them by index but failed.
column I want to split:
[{'action_type': 'link_click', 'value': '1'}, {'action_type': 'video_view', 'value': '1'}]
the state i want to translate:
actions_video_view actions_link_click
1 1
xx = dataframe['actions'].apply(pd.Series).merge(dataframe["index"],
right_index=True,
left_index=True).melt(id_vars=['index'],
value_name='actions')
xx2 = xx['action_type'].apply(pd.Series).merge(xx["index"],
right_index=True,
left_index=True)
xx2 = xx2.loc[xx2['action_type'] == 'video_view', ["value", "index"]]
when i run this code i get the following error:
Traceback (most recent call last):
File "C:ProgramDataAnaconda3libsite-packagespandascoreframe.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "C:ProgramDataAnaconda3libsite-packagespandascoreindexesbase.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 'action_type'
I want to separate the column according to the keys and add it as a dataframe column, which way can I use for this?
An example of how it looks in the data:
actions
[{'action_type': 'link_click', 'value': '1'}, {'action_type': 'video_view', 'value': '1'}]
[{'action_type': 'link_click', 'value': '3'}, {'action_type': 'video_view', 'value': '3'}]
[{'action_type': 'link_click', 'value': '5'}, {'action_type': 'video_view', 'value': '5'}]
[{'action_type': 'link_click', 'value': '6'}, {'action_type': 'video_view', 'value': '6'}]
[{'action_type': 'link_click', 'value': '7'}, {'action_type': 'video_view', 'value': '7'}]
if i want to apply:
actions_link_click actions_video_view
1 1
3 3
5 5
6 6
7 7
2
Answers
This does the job,
Output –
I think you should have a look on how that data is generated like that to each row of a dataframe. I think that is not straightforward. But for your current problem, here is a solution: