使用Pandas将groupby.sum()的结果映射到另一个数据框?

时间:2019-10-08 20:38:47

标签: python pandas

我有两个数据框-一个是微观级别,包含所有交易(DF1)中购买的所有订单项。将构建另一个数据框,以进行更高级别的汇总,以汇总每笔交易产生的收入,从本质上总结每笔交易(DF2)的所有订单项。

df1
Out[df1]:
   transaction_id item_id amount
0  AJGDO-12304     120     $120
1  AJGDO-12304     40      $10
2  AJGDO-12304     01      $10
3  ODSKF-99130     120     $120
4  ODSKF-99130     44      $30
5  ODSKF-99130     03      $50


df2
Out[df2]
    transaction_id    location_id      customer_id   revenue(THIS WILL BE THE ADDED COLUMN!) 
    0  AJGDO-12304          2131234       1234        $140
    1  ODSKF-99130          213124        1345        $200

我该如何链接groupby.sum()的输出并将其分配给df2?收入列实际上是df1 ['transaction_id']的收入汇总,我想将其链接到df2 ['transaction_id']

这是我目前正在尝试的方法,但正在努力拼凑在一起,

results = df1.groupby('transaction_id')['amount'].sum()
df2['revenue'] = df2['transaction_id'].merge(results,how='left').value

2 个答案:

答案 0 :(得分:1)

使用map

 GET indexname/_search
 {
   "_source": ["documentname"]
 }

输出

lookup = df1.groupby(['transaction_id'])['amount'].sum()
df2['revenue'] = df2.transaction_id.map(lookup)

print(df2)

答案 1 :(得分:0)

使用地图:

lookup = df1.groupby(['transaction_id'])['amount'].sum()
df2['revenue'] = df2.transaction_id.map(lookup)

print(df2)
Output

  transaction_id  location_id  customer_id  revenue
0    AJGDO-12304      2131234         1234      140
1    ODSKF-99130       213124         1345      200