门店投资预算实训¶

根据开店计划表(I01)和投资标准成本表(I02)计算每家店的投资额,将建设期作为投资支出时间汇总,得出公司 2019 年每月的门店投资预算(I03),并写回 Excel 工作表。

1. 数据准备¶

In [1]:
import pandas as pd

#设置数据源文件路径
file = 'budget_data.xlsx'
df_I01 = pd.read_excel(file,sheet_name='I01') #开店计划表
df_I02 = pd.read_excel(file,sheet_name='I02',index_col=0) #投资标准成本表

2. 根据开店计划表和投资标准成本表,计算每家店的投资额¶

In [2]:
#对于设计费一直到水电工程费,预算额=每店面积 * 单价
for i in df_I02[df_I02['单位'] == '面积'].index:
    df_I01[i] = df_I01['面积'] * df_I02.loc[i,'单价']
#对于橱柜到电子广告牌,预算额=每店按店规模匹配
for i in df_I02[df_I02['单位'] != '面积'].index:
    df_I01[i] = df_I01['店规模'].apply(lambda x:df_I02.loc[i,x])
#计算投资成本合计
df_I01['Capex']=df_I01.loc[:,'设计':'电子广告牌'].sum(axis=1)

3. 将建设期作为投资支出时间,按建设期汇总单店投资额,得出公司 2019 年每月的门店投资预算¶

In [3]:
#pandas 2.x 中 sum()默认不再自动剔除文本列,需加 numeric_only=True 只汇总数值列(旧版写法为 .sum())
df_I03 = df_I01.groupby(['建设期']).sum(numeric_only=True).T
df_I03
Out[3]:
建设期 2019年2月 2019年3月 2019年4月 2019年7月 2019年9月
面积 1000.0 500.0 600.0 300.0 1150.0
设计 50000.0 25000.0 30000.0 15000.0 57500.0
地板 130000.0 65000.0 78000.0 39000.0 149500.0
照明 6000.0 3000.0 3600.0 1800.0 6900.0
墙面 200000.0 100000.0 120000.0 60000.0 230000.0
水电工程 25000.0 12500.0 15000.0 7500.0 28750.0
橱柜 12000.0 8000.0 8000.0 6000.0 18000.0
吧台 8000.0 6000.0 6000.0 5000.0 13000.0
招牌 3000.0 2500.0 2500.0 2000.0 5000.0
桌椅 36000.0 22000.0 22000.0 12000.0 48000.0
设备 450000.0 300000.0 300000.0 150000.0 600000.0
电子广告牌 20000.0 15000.0 15000.0 10000.0 30000.0
Capex 940000.0 559000.0 600100.0 308300.0 1186650.0

4. 把 df_I03 写入到工作表中¶

In [4]:
#engine='openpyxl'下才有追加写入Excel功能,新版本需要添加if_sheet_exists='replace'参数
with pd.ExcelWriter(file, mode='a',engine='openpyxl',if_sheet_exists='replace') as writer:
    df_I03.to_excel(writer,sheet_name='I03')  #门店投资预算表