python处理单个以及批量处理excel文件

less than 1 minute read

Published:

先贴上数据集,

链接:https://pan.baidu.com/s/1ttv7NwbRmfVPcj2iBHTAfg 提取码:zg5v

下面是关于如何计算每个销售额以及总销售的代码。

import os
import pandas as pd

os.chdir("C:\\Users\\yuyuk\\data science\\data analysis and description\\py_excel")
name= "专项户外运动装备&冰爪.xlsx"
df = pd.read_excel(name)
df.head()
df["销售额"] = df["转化率"] * df["访客数"] * df["客单价"]
df_sum = df.groupby("品牌")["销售额"].sum().reset_index()
df["行业"] = name.replace(".xlsx","")

不同的单个excel计算销售额只需要将name名字换下就好
-------------------------------------------------------------------------------------
以下是计算所有表总的销售额
import os 
import pandas as pd
import time

start_time = time.time()
result = pd.DataFrame()

#循环遍历表格结果
for name in os.listdir():
    df = pd.read_excel(name)
    df["销售额"] = df["访客数"] * df["转化率"] * df["客单价"]
    df_sum = df.groupby("品牌")["销售额"].sum().reset_index()
    df_sum["行业"] = name.replace(".xlsx","")
    result = pd.concat([resutl,df_sum])
    
final = result.groupby("品牌")["销售额"].sum().reset_index().sort_values("销售额",ascending=False)
pd.set_option("display.float_format",lambda x: "%.2f" %x)
print(df_sum.head())
df_sum.describe()

end_time = time.time()
during_time = end_time - start_time
print(during_time)