2. 假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实 际数据。
假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实 际数据。编写程序,完成下面的任务,要求对结果图形进行适当的美化: (1)使用pandas读取文件data.csv中的数据,创建DataFrame对象,并删除其中所有的缺失值; (2)绘制第1个月的营业额折线图,反映饭店每天的营业额情况,并把图形保存为本地文件day.jpg; (3)按月份进行统计,绘制柱状图显示每个月份的营业额,并把图形保存为本地文件month.jpg; (4)按季度统计该饭店2020年的营业额数据,绘制饼状图显示4个季度的营业额分布情况,并把图形保存为本地文件quarter.jpg。
import matplotlib import pandas import matplotlib.pyplot as plt matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # pyplot中文显示 df = pandas.read_csv("data.csv", engine='python') df['日期'] = pandas.to_datetime(df['日期']) df[['交易额']] = df[['交易额']].astype(float) # 将交易额转换为float类型 df = df.dropna() # 去除缺失行 plt.figure() df1 = df.head(31) # 取第一个月 df1.plot(x='日期') # 画图,以日期为x轴 plt.savefig('day.jpg') plt.figure() df2 = df df2['月份'] = df2['日期'].map(lambda x: x.month) # 提取出月份 df2 = df2.groupby(by='月份', as_index=False).sum() # as_inside=False不把month作为新的index,分组求和 df2.plot(x='月份', kind='bar', y='交易额') plt.savefig('month.jpg') plt.figure() one = df2[:3]['交易额'].sum() two = df2[3:6]['交易额'].sum() three = df2[6:9]['交易额'].sum() four = df2[9:12]['交易额'].sum() plt.pie([one, two, three, four], labels=['第一季度', '第二季度', '第三季度', '第四季度']) plt.savefig('quarter.jpg')
日期,交易额 2020/1/1,1886 2020/1/2,1562 2020/1/3,1250 2020/1/4,865 2020/1/5,1250 2020/1/6,316 2020/1/7,607 2020/1/8,487 2020/1/9,293 2020/1/10,450 2020/1/11,525 2020/1/12,791 2020/1/13,711 2020/1/14,876 2020/1/15,685 2020/1/16,968 2020/1/17,1431 2020/1/18,1696 2020/1/19,2013 2020/1/20,2101 2020/1/21,1187 2020/1/22,2865 2020/1/23,2510 2020/1/24,2350 2020/1/25,2034 2020/1/26,1820 2020/1/27,1986 2020/1/28,1176 2020/1/29,1885 2020/1/30,1879 2020/1/31,2056 2020/2/1,1003 2020/2/2,1204 2020/2/3,1045 2020/2/4,1012 2020/2/5,966 2020/2/6,875 2020/2/7,414 2020/2/8,1145 2020/2/9,729 2020/2/10,947 2020/2/11,644 2020/2/12,712 2020/2/13,583 2020/2/14,315 2020/2/15,533 2020/2/16,621 2020/2/17,561 2020/2/18,929 2020/2/19,1264 2020/2/20,749 2020/2/21,964 2020/2/22,864 2020/2/23,449 2020/2/24,1242 2020/2/25,390 2020/2/26,816 2020/2/27,891 2020/2/28,657 2020/2/29,748 2020/3/1,943 2020/3/2,467 2020/3/3,541 2020/3/4,477 2020/3/5,964 2020/3/6,364 2020/3/7,814 2020/3/8,796 2020/3/9,992 2020/3/10,730 2020/3/11,606 2020/3/12,959 2020/3/13,1211 2020/3/14,744 2020/3/15,826 2020/3/16,791 2020/3/17,984 2020/3/18,672 2020/3/19,383 2020/3/20,800 2020/3/21,751 2020/3/22,1377 2020/3/23,1098 2020/3/24,459 2020/3/25,855 2020/3/26,1036 2020/3/27,815 2020/3/28,NaN 2020/3/29,848 2020/3/30,1048 2020/3/31,639 2020/4/1,858 2020/4/2,1070 2020/4/3,685 2020/4/4,1220 2020/4/5,991 2020/4/6,1235 2020/4/7,1316 2020/4/8,208 2020/4/9,458 2020/4/10,1077 2020/4/11,479 2020/4/12,821 2020/4/13,452 2020/4/14,394 2020/4/15,913 2020/4/16,979 2020/4/17,963 2020/4/18,716 2020/4/19,600 2020/4/20,577 2020/4/21,731 2020/4/22,1401 2020/4/23,761 2020/4/24,698 2020/4/25,283 2020/4/26,1255 2020/4/27,338 2020/4/28,1114 2020/4/29,722 2020/4/30,873 2020/5/1,1183 2020/5/2,895 2020/5/3,1063 2020/5/4,823 2020/5/5,380 2020/5/6,712 2020/5/7,449 2020/5/8,910 2020/5/9,603 2020/5/10,944 2020/5/11,267 2020/5/12,1210 2020/5/13,840 2020/5/14,403 2020/5/15,780 2020/5/16,473 2020/5/17,300 2020/5/18,513 2020/5/19,619 2020/5/20,493 2020/5/21,650 2020/5/22,480 2020/5/23,456 2020/5/24,567 2020/5/25,879 2020/5/26,874 2020/5/27,286 2020/5/28,550 2020/5/29,423 2020/5/30,529 2020/5/31,1289 2020/6/1,1087 2020/6/2,510 2020/6/3,548 2020/6/4,1207 2020/6/5,847 2020/6/6,841 2020/6/7,1368 2020/6/8,920 2020/6/9,866 2020/6/10,759 2020/6/11,606 2020/6/12,1472 2020/6/13,563 2020/6/14,795 2020/6/15,NaN 2020/6/16,528 2020/6/17,589 2020/6/18,848 2020/6/19,421 2020/6/20,299 2020/6/21,786 2020/6/22,909 2020/6/23,975 2020/6/24,1132 2020/6/25,372 2020/6/26,1039 2020/6/27,585 2020/6/28,390 2020/6/29,563 2020/6/30,924 2020/7/1,335 2020/7/2,466 2020/7/3,770 2020/7/4,846 2020/7/5,554 2020/7/6,1104 2020/7/7,NaN 2020/7/8,515 2020/7/9,306 2020/7/10,692 2020/7/11,1180 2020/7/12,508 2020/7/13,664 2020/7/14,487 2020/7/15,596 2020/7/16,215 2020/7/17,623 2020/7/18,1075 2020/7/19,868 2020/7/20,508 2020/7/21,838 2020/7/22,216 2020/7/23,305 2020/7/24,1043 2020/7/25,1410 2020/7/26,431 2020/7/27,685 2020/7/28,309 2020/7/29,504 2020/7/30,769 2020/7/31,971 2020/8/1,814 2020/8/2,440 2020/8/3,990 2020/8/4,780 2020/8/5,925 2020/8/6,820 2020/8/7,829 2020/8/8,1227 2020/8/9,356 2020/8/10,594 2020/8/11,793 2020/8/12,622 2020/8/13,888 2020/8/14,981 2020/8/15,1268 2020/8/16,272 2020/8/17,703 2020/8/18,922 2020/8/19,NaN 2020/8/20,601 2020/8/21,913 2020/8/22,537 2020/8/23,709 2020/8/24,387 2020/8/25,594 2020/8/26,517 2020/8/27,830 2020/8/28,889 2020/8/29,1056 2020/8/30,1048 2020/8/31,568 2020/9/1,1310 2020/9/2,596 2020/9/3,1143 2020/9/4,502 2020/9/5,756 2020/9/6,581 2020/9/7,1073 2020/9/8,426 2020/9/9,1113 2020/9/10,646 2020/9/11,1080 2020/9/12,617 2020/9/13,685 2020/9/14,NaN 2020/9/15,811 2020/9/16,742 2020/9/17,838 2020/9/18,1170 2020/9/19,979 2020/9/20,1237 2020/9/21,673 2020/9/22,268 2020/9/23,984 2020/9/24,909 2020/9/25,666 2020/9/26,369 2020/9/27,1357 2020/9/28,932 2020/9/29,563 2020/9/30,2012 2020/10/1,2565 2020/10/2,2157 2020/10/3,2046 2020/10/4,2321 2020/10/5,1966 2020/10/6,1986 2020/10/7,2345 2020/10/8,1117 2020/10/9,999 2020/10/10,1102 2020/10/11,1024 2020/10/12,1135 2020/10/13,962 2020/10/14,975 2020/10/15,663 2020/10/16,823 2020/10/17,NaN 2020/10/18,786 2020/10/19,633 2020/10/20,881 2020/10/21,1161 2020/10/22,522 2020/10/23,471 2020/10/24,586 2020/10/25,589 2020/10/26,1280 2020/10/27,950 2020/10/28,1111 2020/10/29,613 2020/10/30,979 2020/10/31,936 2020/11/1,663 2020/11/2,1140 2020/11/3,751 2020/11/4,1068 2020/11/5,966 2020/11/6,528 2020/11/7,474 2020/11/8,1323 2020/11/9,716 2020/11/10,679 2020/11/11,786 2020/11/12,267 2020/11/13,560 2020/11/14,801 2020/11/15,1060 2020/11/16,685 2020/11/17,992 2020/11/18,984 2020/11/19,884 2020/11/20,514 2020/11/21,879 2020/11/22,1245 2020/11/23,537 2020/11/24,759 2020/11/25,571 2020/11/26,883 2020/11/27,1040 2020/11/28,830 2020/11/29,1019 2020/11/30,1072 2020/12/1,805 2020/12/2,691 2020/12/3,899 2020/12/4,986 2020/12/5,799 2020/12/6,876 2020/12/7,1292 2020/12/8,482 2020/12/9,804 2020/12/10,1028 2020/12/11,817 2020/12/12,415 2020/12/13,498 2020/12/14,1108 2020/12/15,986 2020/12/16,845 2020/12/17,882 2020/12/18,741 2020/12/19,1082 2020/12/20,1280 2020/12/21,519 2020/12/22,739 2020/12/23,792 2020/12/24,714 2020/12/25,1339 2020/12/26,1460 2020/12/27,989 2020/12/28,1053 2020/12/29,1263 2020/12/30,1264 2020/12/31,1123