collect_total_pt=pd.pivot_table(原始数据,index=['纵坐标1','纵坐标2'],columns='横坐标',values='值',aggfunc=聚合方式,fill_value=空值填充方式,margins=True)
将透视表中的数据转换成百分比
collect_total_pt11=collect_total_pt1.copy()
for col_name in collect_total_pt11.columns[2:]:
collect_total_pt11[col_name]=collect_total_pt11[col_name]/collect_total_pt11.loc[12,col_name]
collect_total_pt11[col_name]=collect_total_pt11[col_name].apply(lambda x:format(x,'.2%'))
collect_total_pt11
自定义聚合方式:
def sum_w(S):
return str(round(S.sum()/10000,2))+'w'
pd.pivot_table(result,index=['催收结果'],values=['remain_principal','order_no'],aggfunc={'remain_principal':sum_w,'order_no':'count'},fill_value=None,margins=True)