【问题标题】:Flatten Pandas DataFrame columns展平 Pandas DataFrame 列
【发布时间】:2021-05-07 21:05:29
【问题描述】:

我最近一直在尝试使用 Facebook 的 Insights API。我觉得我快到了。

当前问题是;我不知道如何正确展平我的对象。

这是一个外观示例:

[{'impressions': '10491',
 'date_start': '2021-01-27',
 'clicks': '52',
 'spend': '265.760003',
 'campaign_name': "campaign_xyz",
 'campaign_id': 'campaign_id_123',
 'ad_name': 'ad_name_xyz,
 'ad_id': 'ad_id_123',
 'account_name': 'account_name_xyz',
 'account_id': 'account_id_123',
 'actions': [{'action_type': 'landing_page_view', 'value': '16'},
  {'action_type': 'link_click', 'value': '18'},
  {'action_type': 'offsite_conversion.fb_pixel_custom', 'value': '4'},
  {'action_type': 'offsite_conversion.fb_pixel_lead', 'value': '3'},
  {'action_type': 'offsite_conversion.fb_pixel_purchase', 'value': '9'},
  {'action_type': 'post_reaction', 'value': '4'},
  {'action_type': 'video_view', 'value': '851'},
  {'action_type': 'offsite_conversion.custom.1152415498471708', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.209429190307342', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.212840703113383', 'value': '3'},
  {'action_type': 'offsite_conversion.custom.223430212241510', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.255869695824072', 'value': '1'},
  {'action_type': 'offsite_conversion.custom.2923329694560824', 'value': '1'},
  {'action_type': 'offsite_conversion.custom.305990577082762', 'value': '3'},
  {'action_type': 'offsite_conversion.custom.3190323731012244', 'value': '1'},
  {'action_type': 'offsite_conversion.custom.334777727694135', 'value': '3'},
  {'action_type': 'offsite_conversion.custom.394900471810386', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.603817837150091', 'value': '1'},
  {'action_type': 'offsite_conversion.custom.611337039483406', 'value': '3'},
  {'action_type': 'offsite_conversion.custom.701618050398896', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.738774496765904', 'value': '2'},
  {'action_type': 'offsite_conversion.custom.758364024710917', 'value': '3'},
  {'action_type': 'offsite_conversion.custom.907815113379720', 'value': '1'},
  {'action_type': 'post_engagement', 'value': '873'},
  {'action_type': 'page_engagement', 'value': '873'},
  {'action_type': 'lead', 'value': '3'},
  {'action_type': 'omni_purchase', 'value': '9'},
  {'action_type': 'purchase', 'value': '9'}],
 'date_stop': '2021-01-27',
 'country': 'unknown'}]

如您所见,当我从中创建一个 dataFrame 时,它​​会发送具有操作,所有 action_types 在一列中。

像这样: screenshot

我的目标是让每种操作类型都在自己的列中。

像这样: screenshot

编辑: 试过这个:

df = pd.DataFrame(l)
for val in df['actions'][0]:
    df[val['action_type']] = val['value']
df.drop('actions', axis=1, inplace=True)

第一次转换为数据框,然后将 action_type dict elemenet 转换为列。

但它会导致整个数据帧中的所有值都相同:

screenshot

【问题讨论】:

  • 你能打印你发布的列表的长度吗?当您拥有一个元素时,我的解决方案有效。但我猜你有多个值。
  • 目前,len(df) 为 154。每个都有一组未测试的 action_types。
  • 我已经更新了我的答案:)
  • 非常感谢!我认为它现在有效!非常感谢!

标签: python pandas dataframe flatten


【解决方案1】:

尝试:

df = pd.DataFrame(l)
for val in df['actions'][0]:
    df[val['action_type']] = val['value']
df.drop('actions', axis=1, inplace=True)

第一次转换为数据框,然后将 action_type dict elemenet 转换为列。


尝试:

def func(row):
    d = {}
    for i in row:
        d[i['action_type']]=i['value']
    return pd.Series(d)

df = pd.DataFrame(l)
df1 = pd.concat([df.drop('actions', axis=1),df['actions'].apply(func)], axis=1)

【讨论】:

  • @SebastianBjørnsen:可以发截图吗?
  • 太棒了。很简单。非常感谢!编辑:差不多了。现在我看到数据框中每一行的值都是相同的。
  • @SebastianBjørnsen:没听懂。每行具有相同的值??如果您有多行,那么您可能需要分组,然后按照此步骤操作。我根据你给我的数据发布了答案。如果您只是 groupby 有多个展示次数,请尝试添加此步骤。创建自定义函数并使用apply
  • 我刚刚用截图对帖子进行了编辑。每行都是唯一的,不应在此处分组。很抱歉造成混淆,并没有解释数据框中有多行开头。
猜你喜欢
  • 2018-09-24
  • 2019-11-05
  • 2018-09-02
  • 1970-01-01
  • 2018-12-11
  • 2016-12-01
  • 1970-01-01
  • 2021-12-03
  • 2017-11-28
相关资源
最近更新 更多