Pandas生成结构式报告
import numpy as np
import pandas as pd
import sys
import matplotlib.pyplot as plt
plt.close('all')
# 任务1:绘制按月份的销售额
dataPath = r'D:\\\\data.csv'
df =pd.read_csv(dataPath)
df.columns = ['num','time','delt','from','fromDes','ip','date','name',
'p6688','p4011','p1012','p1013','p1025',
'p1026','p4666','p4777','p4000','p4324',
'pDuo','image','note','Amount']
df=df[~df['date'].str.contains('空')] #删除某列包含特殊字符的行
#清理一下空数据
df['date'] = pd.to_datetime(df['date']) # 将数据类型转换为日期类型
df = df.set_index('date') # 将date设置为index
s = pd.Series(df['Amount'], index=df.index)#取出来销量,这一列
# r = s.resample('w').sum().to_period('w').head(sys.maxsize)
# r = s.resample('MS').sum().to_period('M').head(sys.maxsize)
#接下来就是plot了
# ax = r.plot.hist(bins=20)
#每一天的销量情况
r = s.resample('D').sum().to_period('D').head(sys.maxsize)
lines = r.plot.line()
plt.show()
f = lines.get_figure()
f.savefig('./PythonScript/myplot1.png')
#每一周的销量情况
r = s.resample('w').sum().to_period('w').head(sys.maxsize)
lines = r.plot.line()
plt.show()
f = lines.get_figure()
f.savefig('./PythonScript/myplot2.png')
#每一月的销量情况
r = s.resample('MS').sum().to_period('M').head(sys.maxsize)
lines = r.plot.line()
plt.show()
f = lines.get_figure()
f.savefig('./PythonScript/myplot3.png')
#每一季度的销量情况
r = s.resample('QS').sum().to_period('Q').head(sys.maxsize)
lines = r.plot.line()
plt.show()
f = lines.get_figure()
f.savefig('./PythonScript/myplot4.png')
#任务2:生成结构式报告。
from reportlab.lib.colors import blue
from reportlab.lib.pagesizes import LETTER,A4
from reportlab.lib.units import inch
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen.canvas import Canvas
canvas = Canvas(".//Financial Statements.pdf", pagesize=LETTER)
canvas.setFont("Times-Roman", 25)
canvas.setFillColor(blue)
w = 64*6
h = 48*3
logo = ImageReader(r"C:\\\Desktop\logo11w.png")
canvas.drawImage(logo, 0.5*inch, 10.3*inch, width=53,height=19,mask='auto')
canvas.drawString(4 * inch, 10 * inch, r"Results")
canvas.drawString(1 * inch, 8 * inch, r"1")
logo = ImageReader(r"./PythonScript/myplot1.png")
canvas.drawImage(logo, 2 * inch, 7.7*inch, width=w,height=h,mask='auto')
canvas.drawString(1 * inch, 6 * inch, r"2")
logo = ImageReader(r"./PythonScript/myplot2.png")
canvas.drawImage(logo, 2 * inch, 5.7*inch, width=w,height=h,mask='auto')
canvas.drawString(1 * inch, 4 * inch, r"3")
logo = ImageReader(r"./PythonScript/myplot3.png")
canvas.drawImage(logo, 2 * inch, 3.7*inch, width=w,height=h,mask='auto')
canvas.drawString(1 * inch, 2 * inch, r"4")
logo = ImageReader(r"./PythonScript/myplot4.png")
canvas.drawImage(logo, 2 * inch, 1.7*inch, width=w,height=h,mask='auto')
canvas.save()