pandas对超市销售数据处理与分析

1、查看单日交易额最小的3天的交易数据,并查看这3天是周几;

import pandas as pd
import time
data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
day=data.loc[:,['日期','交易额']].groupby('日期',as_index=False).sum()
day=day.nsmallest(3,'交易额')
day['weekday']=pd.to_datetime(day['日期']).dt.weekday+1
print(day)

2、把所有员工的工号前面增加一位数字,增加的数字和原工号最后一位相同,把修改后的数据写入新的文件“超市营业额2_修改工号.xlsx”,例如:工号1001变为11001,1003变为31003;

import pandas as pd

def addstr(s):
    return str(s)[-1]+str(s)

data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
data['工号']=data['工号'].map(addstr)
data.to_excel('E:/Python/超市营业额2_修改工号.xlsx')

 3、把每个员工的交易额数据写入文件“各员工数据.xlsx”,每个员工的数据占一个 worksheet,结构和“超市营业额2.xlsx”一样,并以员工姓名作为worksheet 的标题;

import pandas as pd
data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
writer=pd.ExcelWriter("E:/Python/各员工数据.xlsx")
names=set(data['姓名'])
for name in names:            
    n=data[data.姓名==name]
    n.to_excel(writer,sheet_name=name,index=False)
writer.save()

 4、利用Pandas和Matplotlib绘制折线图展示一个月内各柜台营业额每天的变化趋势;

import pandas as pd
import matplotlib as plt
plt.rcParams['font.sans-serif']=['simhei']
data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
data=data.loc[:,['日期','柜台','交易额']].groupby(by=['日期','柜台'],as_index=False).sum()
data=data.pivot(index='日期',columns='柜台',values='交易额')
data.plot()

5、利用Pandas和Matplotlib绘制饼状图展示该月各柜台营业额在交易总额中的占比; 

import pandas as pd
import matplotlib as pltS
plt.rcParams['font.sans-serif']=['simhei']
data=data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
data=data.loc[:,['柜台','交易额']].groupby('柜台',as_index=False).sum()
data.plot(x='柜台',y='交易额',kind='pie',labels=data.柜台.values)

6、利用Pandas和Matplotlib绘制柱状图展示张三在不同柜台的交易总额。

import pandas as pd
import matplotlib as plt
plt.rcParams['font.sans-serif']=['simhei']
data=data=pd.read_excel('E:/Python/超市营业额2.xlsx','Sheet1')
data=data.loc[:,['姓名','柜台','交易额']].groupby(by=['姓名','柜台'],as_index=False).sum()
data=data[data.姓名=='张三']
data.plot(x='柜台',y='交易额',kind='bar')
print(data)

你可能感兴趣的:(pandas,python,开发语言)