如何从 facebook 操作数据集中提取见解并将所有值转换为每一列
How to extract insights from facebook action dataset and covert all values into each column
我想在列中附加值,我试过这段代码
y = data['actions'].apply(lambda x: str(x).replace("'",'"'))
json.loads(y[0])
json.loads(y[1])
它给出如下所示的输出
[{'action_type': 'post_reaction', 'value': '2'},
{'action_type': 'link_click', 'value': '42'},
{'action_type': 'comment', 'value': '1'},
{'action_type': 'post_engagement', 'value': '45'},
{'action_type': 'page_engagement', 'value': '45'},
{'action_type': 'onsite_conversion.lead_grouped', 'value': '6'},
{'action_type': 'leadgen_grouped', 'value': '6'},
{'action_type': 'lead', 'value': '6'}]
[{'action_type': 'onsite_conversion.post_save', 'value': '1'},
{'action_type': 'post_reaction', 'value': '4'},
{'action_type': 'link_click', 'value': '62'},
{'action_type': 'post_engagement', 'value': '67'},
{'action_type': 'page_engagement', 'value': '67'},
{'action_type': 'onsite_conversion.lead_grouped', 'value': '6'},
{'action_type': 'leadgen_grouped', 'value': '6'},
{'action_type': 'lead', 'value': '6'}]
我想创建一个数据框,将每个动作类型作为列,并将它们的值附加到相应的列中,如果没有值,它附加零,如
| post_reaction| link click | comment |---------------------
| -------- | -----------|---------|
| 2 | 42 |1 |
| 4 | 62 |67 |
如果数据中没有列表,首先使用 ast.literal_eval
进行转换,然后使用列表理解创建 DataFrame
:
import ast
y = data['actions'].apply(ast.literal_eval)
df = pd.DataFrame([{z['action_type']:z['value'] for z in x} for x in y]).fillna(0)
如果数据中的列表仅使用列表理解:
df = (pd.DataFrame([{z['action_type']:z['value'] for z in x} for x in data['actions']])
.fillna(0))
我想在列中附加值,我试过这段代码
y = data['actions'].apply(lambda x: str(x).replace("'",'"'))
json.loads(y[0])
json.loads(y[1])
它给出如下所示的输出
[{'action_type': 'post_reaction', 'value': '2'},
{'action_type': 'link_click', 'value': '42'},
{'action_type': 'comment', 'value': '1'},
{'action_type': 'post_engagement', 'value': '45'},
{'action_type': 'page_engagement', 'value': '45'},
{'action_type': 'onsite_conversion.lead_grouped', 'value': '6'},
{'action_type': 'leadgen_grouped', 'value': '6'},
{'action_type': 'lead', 'value': '6'}]
[{'action_type': 'onsite_conversion.post_save', 'value': '1'},
{'action_type': 'post_reaction', 'value': '4'},
{'action_type': 'link_click', 'value': '62'},
{'action_type': 'post_engagement', 'value': '67'},
{'action_type': 'page_engagement', 'value': '67'},
{'action_type': 'onsite_conversion.lead_grouped', 'value': '6'},
{'action_type': 'leadgen_grouped', 'value': '6'},
{'action_type': 'lead', 'value': '6'}]
我想创建一个数据框,将每个动作类型作为列,并将它们的值附加到相应的列中,如果没有值,它附加零,如
| post_reaction| link click | comment |---------------------
| -------- | -----------|---------|
| 2 | 42 |1 |
| 4 | 62 |67 |
如果数据中没有列表,首先使用 ast.literal_eval
进行转换,然后使用列表理解创建 DataFrame
:
import ast
y = data['actions'].apply(ast.literal_eval)
df = pd.DataFrame([{z['action_type']:z['value'] for z in x} for x in y]).fillna(0)
如果数据中的列表仅使用列表理解:
df = (pd.DataFrame([{z['action_type']:z['value'] for z in x} for x in data['actions']])
.fillna(0))