python - Pandas - 按列值将数据框拆分为多个 Excel 工作簿
问题描述
我是熊猫新手。我有一个大的 excel 文件,我想要做的是将操作后的数据框拆分为多个 excel 工作簿。大约有 400 个供应商,我希望每个供应商都有自己的命名工作簿。
例子。SallyCreative.xlsx、JohnWorks.xlsx、AlexGraphics.xlsx
解决方案
这是我按列值将数据框拆分为多个 Excel 工作簿的方法。
import pandas as pd
data = pd.read_excel('anyexcelfile.xlsx', engine='openpyxl') # creates a dataframe called 'data'; pick any spreadsheet you can add paths to 'x:/folder/subfolder/anyexcelfile.xlsx' to be explict.
grouped = data.groupby("Column Header Name") # change "Column Header Name" to the name of the column needed to categorise or group the rows in the dataframe,
keys = grouped.groups.keys() #create a dictionary list of the each group unique varibles in the specifed column of the dataframe.
print(keys) #a cheeky debug to check it's working
for key in keys: #looping through each key
splitdf = grouped.get_group(key) # creating a temporary dataframe with only the values of the current key.
splitdf.to_excel(str(key)+".xlsx", engine='xlsxwriter') #write the temporary dataframe called 'splitdf' to an excel file named after the key. At the end of the loop the temporary dataframe 'splitdf' is overwritten for use with the next key.
推荐阅读
- kotlin - Kotlin:当枚举名称明确时省略枚举名称
- qgis - 无法使用代码块和 vscode ide 编译 qgis 项目
- firebase - Firebase 托管区分大小写在本地有效,但在托管时无效
- pandas - 在 Pandas Dataframe 中解析日期时间
- linux - 即使间隔设置为零,使用 timerfd_create() 和 timerfd_settime() 的计时器也会持续到期
- javascript - 如何使用 ng-href 而不是 ng-click 制作超链接并路由到 Angular 2.2 中的不同状态?
- sql - 标记他们返回的日期
- nlp - 如何自动查找与给定列表中相似的句子?
- python - 使用 matplotlib 在 3D 图形上交互式选择点
- mysql - MariaDB - 如何在 POINT 字段中查询给定城市/区域的纬度/经度坐标内的位置?