1 Star 2 Fork 1

万万没想到 / python-for-excel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sales_report_pandas.py 930 Bytes
一键复制 编辑 原始数据 按行查看 历史
Felix Zumstein 提交于 2021-02-18 17:11 . initial commit
from pathlib import Path
import pandas as pd
# Directory of this file
this_dir = Path(__file__).resolve().parent
# Read in all Excel files from all subfolders of sales_data
parts = []
for path in (this_dir / "sales_data").rglob("*.xls*"):
print(f'Reading {path.name}')
part = pd.read_excel(path, index_col="transaction_id")
parts.append(part)
# Combine the DataFrames from each file into a single DataFrame
# pandas takes care of properly aligning the columns
df = pd.concat(parts)
# Pivot each store into a column and sum up all transactions per date
pivot = pd.pivot_table(df,
index="transaction_date", columns="store",
values="amount", aggfunc="sum")
# Resample to end of month and assign an index name
summary = pivot.resample("M").sum()
summary.index.name = "Month"
# Write summary report to Excel file
summary.to_excel(this_dir / "sales_report_pandas.xlsx")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/absolutely_unexpected/python-for-excel.git
git@gitee.com:absolutely_unexpected/python-for-excel.git
absolutely_unexpected
python-for-excel
python-for-excel
1st-edition

搜索帮助

344bd9b3 5694891 D2dac590 5694891