繁体   English   中英

使用 Python 根据第一列将 xlsx 文件拆分为其他 xlsx 文件

[英]Split xlsx file into other xlsx files based on first column with Python

我有一个很大的 .xlsx 文件,我想根据第一列将其拆分为多个文件。 数据结构有点不确定,这使它变得很复杂,而且我是 Python 中的佼佼者。

基本上,我需要为以“Brand1”(见下文)开头的每一行创建一个单独的文件,并为以“Brand2”开头的行创建另一个文件,依此类推。 什么是最好的方法来做到这一点?

Brand_name               Sales                 Year
  BRAND1                1000000                2018 
  BRAND1                1100000                2019
  BRAND2                 900000                2018
  BRAND2                 500000                2019
  BRAND3                 200000                2019
  BRAND4                1200000                2019

您可能想要这样做。

# list all the brands (unique values)
brands = list(df['Brand_name'].unique())

# for each brand, we want to filter the dataframe based on the brand name
# and then export it to excel
for brand in brands:
  filtered_df = df[df['Brand_name'] == brand]
  filtered_df.to_excel(brand + '.xlsx', index=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM