python 表格转换(数据透视)

1、导入数据

import pandas as pd
import numpy as np

import pandas as pd
table = pd.DataFrame({'cust_id':[10001,10001,10002,10002,10003],
                      'type':['Normal','Special_offer',\
                              'Normal','Special_offer','Special_offer'],
                      'Monetary':[3608,420,1894,3503,4567]})
Out[3]: 
   Monetary  cust_id           type
0      3608    10001         Normal
1       420    10001  Special_offer
2      1894    10002         Normal
3      3503    10002  Special_offer
4      4567    10003  Special_offer

表格形式转换(数据透视)

result=pd.pivot_table(table,index='cust_id',columns='type',values='Monetary')
Out[5]: 
type     Normal  Special_offer
cust_id                       
10001    3608.0          420.0
10002    1894.0         3503.0
10003       NaN         4567.0
pd.pivot_table(table,index='cust_id',columns='type',values='Monetary',
        fill_value=0,aggfunc='sum')
Out[6]: 
type     Normal  Special_offer
cust_id                       
10001      3608            420
10002      1894           3503
10003         0           4567
table1 = pd.pivot_table(table,index='cust_id',
                        columns='type',
                        values='Monetary',
                        fill_value=0,
                        aggfunc=np.sum).reset_index()
type  cust_id  Normal  Special_offer
0       10001    3608            420
1       10002    1894           3503
2       10003       0           4567
pd.melt(table1,
	id_vars='cust_id',
value_vars=['Normal','Special_offer'],
value_name='Monetary',
var_name='TYPE')
Out[8]: 
   cust_id           TYPE  Monetary
0    10001         Normal      3608
1    10002         Normal      1894
2    10003         Normal         0
3    10001  Special_offer       420
4    10002  Special_offer      3503
5    10003  Special_offer      4567

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值