Pandas Pivot表 - 添加具有熔解函数的新行

时间:2016-11-18 14:34:28

标签: pandas pivot melt

我希望有人能告诉我如何完成以下步骤:

我有以下数据框:

df = [{'Failed by Fraud' : 3, 'Failed by Bot': 7, 'Failed by Geography':4, 'Failed by Suspicious Activity' : 2, 'Impressions': 22, 'Clicks':12, 'Date': '24/07/2016', "ID": 34},]

我使用以下代码在Pandas中取消它:

pd.melt(df, id_vars=["Date", "Impressions", "Clicks", "ID"], var_name='Reason of Faiure', value_name='Failures')

结果如下:

    Date       Impressions  Clicks  ID  Reason of Faiure    Failures
0   24/07/2016  22          12      34  Failed by Bot       7
1   24/07/2016  22          12      34  Failed by Fraud     3
2   24/07/2016  22          12      34  Failed by Geography 4
3   24/07/2016  22          12      34  Failed by Suspicious Activity   2

但是,我想要的是以下结果:

    Date       Impressions  Clicks  ID  Reason of Faiure    Failures
0   24/07/2016  22          12      34  NaN                 NaN
1   24/07/2016  NaN         NaN     34  Fraud               3
2   24/07/2016  NaN         NaN     34  Geography           4
3   24/07/2016  NaN         NaN     34  Suspicious Activity 2
4   24/07/2016  NaN         NaN     34  Bot                 3

这意味着,换句话说,添加1行,显示ID按当天收到的展示次数和点击次数,其中包含失败和失败原因的空值。另一方面,展示次数和点击次数为空值,但包含ID,失败原因和失败的值

0 个答案:

没有答案