Python 3.x 在Pandas和Numpy中合并数据帧
我有两个关于销售分析的不同数据框架。我想将它们合并在一起,形成一个新的数据框架,其中包含customer\u id、name和total\u expense列。这两个数据帧如下所示:Python 3.x 在Pandas和Numpy中合并数据帧,python-3.x,pandas,numpy,dataframe,Python 3.x,Pandas,Numpy,Dataframe,我有两个关于销售分析的不同数据框架。我想将它们合并在一起,形成一个新的数据框架,其中包含customer\u id、name和total\u expense列。这两个数据帧如下所示: import pandas as pd import numpy as np customers = pd.DataFrame([[100, 'Prometheus Barwis', 'prometheus.barwis@me.com', '(533) 072-2779'],[101, 'Alai
import pandas as pd
import numpy as np
customers = pd.DataFrame([[100, 'Prometheus Barwis', 'prometheus.barwis@me.com',
'(533) 072-2779'],[101, 'Alain Hennesey', 'alain.hennesey@facebook.com',
'(942) 208-8460'],[102, 'Chao Peachy', 'chao.peachy@me.com',
'(510) 121-0098'],[103, 'Somtochukwu Mouritsen',
'somtochukwu.mouritsen@me.com','(669) 504-8080'],[104,
'Elisabeth Berry', 'elisabeth.berry@facebook.com','(802) 973-8267']],
columns = ['customer_id', 'name', 'email', 'phone'])
orders = pd.DataFrame([[1000, 100, 144.82], [1001, 100, 140.93],
[1002, 102, 104.26], [1003, 100, 194.6 ], [1004, 100, 307.72],
[1005, 101, 36.69], [1006, 104, 39.59], [1007, 104, 430.94],
[1008, 103, 31.4 ], [1009, 104, 180.69], [1010, 102, 383.35],
[1011, 101, 256.2 ], [1012, 103, 930.56], [1013, 100, 423.77],
[1014, 101, 309.53], [1015, 102, 299.19]],
columns = ['order_id', 'customer_id', 'order_total'])
当我按客户id和订单id分组时,我得到下表:
customer_id order_id order_total
100 1000 144.82
1001 140.93
1003 194.60
1004 307.72
1013 423.77
101 1005 36.69
1011 256.20
1014 309.53
102 1002 104.26
1010 383.35
1015 299.19
103 1008 31.40
1012 930.56
104 1006 39.59
1007 430.94
1009 180.69
这就是我被卡住的地方。我不知道如何对每个客户id的所有订单进行汇总,从而形成一个总花费列。如果有人知道这样做的方法,将不胜感激 IIUC,您可以执行以下操作
orders.groupby('customer_id')['order_total'].sum().reset_index(name='Customer_Total')
输出
customer_id Customer_Total
0 100 1211.84
1 101 602.42
2 102 786.80
3 103 961.96
4 104 651.22
IIUC,你可以做下面的事情
orders.groupby('customer_id')['order_total'].sum().reset_index(name='Customer_Total')
输出
customer_id Customer_Total
0 100 1211.84
1 101 602.42
2 102 786.80
3 103 961.96
4 104 651.22
您可以创建一个附加表,然后
合并
返回到当前输出
# group by customer id and order id to match your current output
df = orders.groupby(['customer_id', 'order_id']).sum()
# create a new lookup table called total by customer
totalbycust = orders.groupby('customer_id').sum()
totalbycust = totalbycust.reset_index()
# only keep the columsn you want
totalbycust = totalbycust[['customer_id', 'order_total']]
# merge bcak to your current table
df =df.merge(totalbycust, left_on='customer_id', right_on='customer_id')
df = df.rename(columns = {"order_total_x": "order_total", "order_total_y": "order_amount_by_cust"})
# expect output
df
您可以创建一个附加表,然后
合并
返回到当前输出
# group by customer id and order id to match your current output
df = orders.groupby(['customer_id', 'order_id']).sum()
# create a new lookup table called total by customer
totalbycust = orders.groupby('customer_id').sum()
totalbycust = totalbycust.reset_index()
# only keep the columsn you want
totalbycust = totalbycust[['customer_id', 'order_total']]
# merge bcak to your current table
df =df.merge(totalbycust, left_on='customer_id', right_on='customer_id')
df = df.rename(columns = {"order_total_x": "order_total", "order_total_y": "order_amount_by_cust"})
# expect output
df
结果:
total_spend
customer_id name
100 Prometheus Barwis 1211.84
103 Somtochukwu Mouritsen 961.96
102 Chao Peachy 786.80
104 Elisabeth Berry 651.22
101 Alain Hennesey 602.42
逐步解释:
how
参数设置为left,因为默认的合并类型是internal(这将忽略没有订单的客户)。
这一步需要对SQL风格的合并方法有一些基本的了解。您可以在中找到各种合并类型的良好视觉概述
.groupby()
文档链接提供了更多关于这方面的示例。同样值得一提的是,这是一种在pandas文档中被称为“split-apply-combine”的模式
列
参数,将数值列从order_total重命名为total_spend total_spend
customer_id name
100 Prometheus Barwis 1211.84
103 Somtochukwu Mouritsen 961.96
102 Chao Peachy 786.80
104 Elisabeth Berry 651.22
101 Alain Hennesey 602.42
逐步解释:
how
参数设置为left,因为默认的合并类型是internal(这将忽略没有订单的客户)。
这一步需要对SQL风格的合并方法有一些基本的了解。您可以在中找到各种合并类型的良好视觉概述
.groupby()
文档链接提供了更多关于这方面的示例。同样值得一提的是,这是一种在pandas文档中被称为“split-apply-combine”的模式
列
参数,将数值列从order_total重命名为total_spend我希望这能有所帮助。看起来您的分组级别比需要的级别多了一个级别-您是如何做到的?你到底想要什么?是这样的:
customers['total\u-spend']=customers['customer\u-id'].map(orders.groupby('customer\u-id')['order\u-total'].sum())
?我通过执行customer\u-spend=pd.merge(customers,orders)customer\u-spend.groupby(['customer\u-id','order\u-id')).sum()得到上表
最终,我想要的是一个最终的表格,该表格将给出客户id、姓名以及此人总共花费了多少钱(因此出现了新的“总花费”列),而上面的内容是否没有做到这一点?您的问题和答案是:您的预期产出是什么?您似乎比必要的级别多了一个级别-您是如何做到的?你到底想要什么?是这样的:customers['total\u-spend']=customers['customer\u-id'].map(orders.groupby('customer\u-id')['order\u-total'].sum())
?我通过执行customer\u-spend=pd.merge(customers,orders)customer\u-spend.groupby(['customer\u-id','order\u-id')).sum()得到上表
最终,我想要的是一个最终的表格,该表格将给出客户id、姓名以及该人总共花费了多少钱(因此出现了新的“总花费”列),而上面的表格并没有这样做吗?您的问题和答案已经找到,您的预期产出是多少?