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