1.将超市销售excel文件根据商品的类别筛选存储
import pandas as pd
d = pd.read_excel("xlscl/step1/超市销售数据.xlsx", dtype={"商品编码":str, "商品条码":str})
l = pd.ExcelWriter("xlscl/step1/类别销售.xlsx")
x = d['类别'].unique()
for i in x:
n = d.loc[d['类别']==i]
n.to_excel(l, sheet_name = i, columns=['日期', '商品编码', '商品名称', '合计金额'])
l.save()
2.将银行信息excel文件按地区筛选存储
import pandas
l = pandas.ExcelWriter('test/银行一线城市.xlsx')
d = pandas.read_excel("test/银行信息.xlsx", dtype={"银行编号":str})
x = ['北京市', '上海市', '广州市', '深圳市']
for i in x:
n = d.loc[d['城市']==i]
n = n.sort_values(by=['银行编号'])
n.to_excel(l, sheet_name=i, columns=['银行编号', '名称'], index=False)
l.save()
3.将gdpecxcel文件按年份筛选存储
import pandas
l = pandas.ExcelWriter('test/GDP分年份.xlsx')
d = pandas.read_excel("test/各省GDP.xlsx", dtype={"年份":str})
s = d['年份'].unique()
l1 = []
for i in s:
if int(i) >= 2000:
l1.append(i)
for i in l1:
n = d.loc[d['年份']==i]
n = n.sort_values(['GDP'], ascending=False)
n.to_excel(l, sheet_name=i, columns=['省份', 'GDP'], index=False)
l.save()
4.统计超市销售excel文件各类别和各日的数据,并将统计结果存入新的工作簿
import pandas as pd
d = pd.read_excel("xlscl/step1/超市销售数据.xlsx")
l = pd.ExcelWriter('xlscl/step2/统计数据.xlsx')
l1 = []
x = list(d['类别'].unique())
for i in x:
n = d.loc[d['类别']==i]
s = n['合计金额'].sum()
l1.append([i,s])
a = pd.DataFrame(l1, columns=['类别', '合计金额'])
a = a.sort_values(by=['合计金额'], ascending=False)
a.to_excel(l, sheet_name='类别统计', index=False)
l2 = []
x = list(d['日期'].unique())
for i in x:
n = d.loc[d['日期']==i]
s = n['合计金额'].sum()
l2.append([i,s])
a = pd.DataFrame(l2, columns=['日期', '合计金额'])
a = a.sort_values(by=['日期'])
a.to_excel(l, sheet_name='日期统计', index=False)
l.save()
5. 将超市销售excel文件分别存放在多个日期工作簿的不同类别工作表中
import pandas as pd
d = pd.read_excel("xlscl/step1/超市销售数据.xlsx", dtype={"商品编码":str, "商品条码":str})
x = d['日期'].unique()
s = d['类别'].unique()
for i in x:
i = str(i).replace('-','')[:8]
l = pd.ExcelWriter('xlscl/step3/rq/{}.xlsx'.format(i))
a = d.loc[d['日期']==i]
k = []
for j in s:
b = a.loc[a['类别']==j]
b.to_excel(l, sheet_name=j, columns=['商品编码', '商品名称', '合计金额'], index=False)
c = b['合计金额'].sum()
if c != 0:
k.append([j, c])
h = pd.DataFrame(k, columns=['类别', '合计金额'])
h = h.sort_values(by=['合计金额'], ascending=False)
h.to_excel(l, sheet_name='类别统计', columns=['类别', '合计金额'], index=False)
l.save()