【python 分组求和功能】类似透视表的作用

# encoding: utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


import time
import pandas as pd
import xlsxwriter


time1 = time.time()
import numpy as np
#############读取数据##########################
data=pd.read_excel('C:/pic/sample.xlsx')


###############算一次消费金额#################
data['sum1']=data['Qty']*data['Sale Price']

# print data

###########分组算每个客户的消费总和###########

a1=pd.pivot_table(data=data,index=['Customer Account'],values=['sum1'],fill_value=0,aggfunc=np.sum)


print a1

#######################输出结果到excel##################
workbook = xlsxwriter.Workbook('F:/result.xlsx')
worksheet = workbook.add_worksheet()



title=[u'Customer Account',u'sum1']

format=workbook.add_format()
format.set_border(1)
format_title = workbook.add_format()
format_title.set_border(1)
format_title.set_bg_color('#cccccc')
format_title.set_align('center')
format_title.set_bold()

format_ave = workbook.add_format()
format_ave.set_border(1)
format_ave.set_num_format('0.00')
worksheet.write_row('A1',title,format_title)
worksheet.write_column('A2',a1.index,format)

worksheet.write_column('B2',a1.iloc[:,0],format)
workbook.close()

time2=time.time()
print u'ok,计算结束!'
print u'总共耗时:'+str(time2-time1)+'s'

"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/数据分组.py
                        sum1
Customer Account            
C0044000034998       76.1300
C0044000035045       54.2700
C0044000071290       68.6839
C0044101000025       27.8923
C0044101000028       10.0660
C0044101000034        3.5800
C0044101000068       34.0180
C0044101000139       15.9700
C0044101000161        7.8600
C0044101000197        5.3900
C0044101000201       66.3108
C0044101000251       40.4034
C0044101000253       12.5700
C0044101000270        9.2000
C0044101000305       15.3700
C0044101000351       44.1672
C0044101000354       55.1500
C0044101000394       22.3300
C0044101000548        5.4500
C0044101000562      193.2898
C0044101000629       16.5400
C0044101000660        7.4900
C0044101000685        6.6930
C0044101000693       59.8100
C0044101000762       11.7400
C0044101000798       44.9855
C0044101000846       16.1400
C0044101000901       20.5704
C0044101000934       29.8049
C0044101000967        4.6155
...                      ...
C0044101049300       60.6874
C0044101049353       64.7245
C0044101049361       27.6100
C0044101049430       19.3200
C0044101049434       36.0761
C0044101049436       30.4483
C0044101049460       17.6300
C0044101049474       86.9000
C0044101049483       62.0900
C0044101049489        8.0727
C0044101049504       18.7800
C0044101049543       48.5459
C0044101049571       30.3638
C0044101050645       15.0000
C0044101050696       46.8700
C0044101050753      178.2900
C0044101050795       38.8966
C0044101050855       31.1700
C0044101051557        5.4100
C0044101051561        4.6644
C0044101051564       29.3200
C0044101051571       23.9888
C0044101051592       11.1187
C0044101051593        6.9900
C0044101051595       25.5268
C0044101051671       21.0400
C0044200020627      124.2040
C0044200020705       10.9999
C0044200022743        9.9900
CSYSTEMID         21507.0180

[387 rows x 1 columns]
ok,计算结束!
总共耗时:1.22399997711s

Process finished with exit code 0
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页