簡體   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