1.计算 业绩提成 汇总计算,运用sum函数对整列进行计算
import pandas as pd
df = pd.read_excel(‘deta/产品销售数据表.xlsx’)
df[‘销售总额(元)’] = df[‘单价(元)’] df[‘销售数量’]
df[‘销售排名’] = df[‘销售数量’].rank(ascending=False).astype(“int”)
def calc(x):
if (x>=50000):
return x0.05
if (x>=30000):
return x0.03
return x0.02
df[‘业绩提成’] = df[‘销售总额(元)’].apply(calc)
print(df[‘销售总额(元)’].sum())
print(df[‘销售数量’].sum())
df.head()
![}HMIVCGTG{_LV793F5({WY.png
2.设置和重置索引 **把员工编号作为索引进行查找
import pandas as pd
df=pd.read_excel(‘产品销售数据表.xlsx’)
df.set_index(‘员工编号’,inplace=True)
print(df.head(3))
![90V9P6D@Q%I86B3C@NU}_K.png**
3.二维表的切片(整行切取)
import pandas as pd
df=pd.readexcel(‘产品销售数据表.xlsx’)
df.set_index(‘员工编号’,inplace=True)
print(df.loc[‘CC801’:’CC803’])**![A@Q(X9@D$$]G@D1}@0$HDP.png](https://cdn.nlark.com/yuque/0/2020/png/1004038/1604453067110-f6428cf6-9add-4ded-956c-ea82a627582a.png#align=left&display=inline&height=555&margin=%5Bobject%20Object%5D&name=A%40Q%28X9%40D%24%24%5DG_%40D1%7D%400%24HDP.png&originHeight=555&originWidth=1079&size=79470&status=done&style=none&width=1079)**
**
4、获取表的全部内容importpandasaspddf=pd.read_excel(‘日常费用统计表.xlsx’)print(df)运行结果:
import pandas as pd
df = pd.read_excel(‘deta/日常费用统计表.xlsx’)
print(df.head())
5、获取默认汇总内容print(df.groupby(by=’费用项目’).sum())运行结果:
import pandas as pd
df = pd.read_excel(‘deta/日常费用统计表.xlsx’)
df.groupby(by=’费用项目’).sum()
6、按照金额大小进行排序汇总print(df.groupby(by=’费用项目’).sum().sort_values(‘金额(元)’,ascending=False))运行结果:
import pandas as pd
df = pd.read_excel(‘deta/日常费用统计表.xlsx’)
df.groupby(by=’费用项目’).sum().sort_values(‘金额(元)’,ascending=False)
7、返回的对象是DataFrame类型print(df.groupby(by=’费用项目’).count())运行结果:
import pandas as pd
df = pd.read_excel(‘deta/日常费用统计表.xlsx’)
df.groupby(by=’费用项目’).count()
![Y{36]Z0~H[Y(@FSU{JOSKL.png](https://cdn.nlark.com/yuque/0/2020/png/1004038/1604459721233-e385b763-d53a-467d-9473-5744d8c48eed.png#align=left&display=inline&height=836&margin=%5Bobject%20Object%5D&name=Y%7B36%5DZ0~H%60%5BY%28%40FSU%7BJOSKL.png&originHeight=836&originWidth=1412&size=121003&status=done&style=none&width=1412)<br />**8、获取汇总所有费用项目的总金额print(df.groupby(by='费用项目').sum()['金额(元)'].sum())运行结果:**<br />import pandas as pd<br />df = pd.read_excel('deta/日常费用统计表.xlsx')<br />df.groupby(by='费用项目')['金额(元)'] .sum()<br />![ZQVIAG7@$6RDNT
~AZ]2`C9.png](https://cdn.nlark.com/yuque/0/2020/png/1004038/1604459916893-8f3e36ca-0312-4847-a850-d80d46eec4f3.png#align=left&display=inline&height=836&margin=%5Bobject%20Object%5D&name=ZQVIAG7%40%246RDNT%60~AZ%5D2%60C9.png&originHeight=836&originWidth=1434&size=131170&status=done&style=none&width=1434)
**