简体   繁体   English

将多个文件中的特定行分组,并将每组行保存在一个新的 excel 文件中,其中包含 python(pandas,openpyxl)

[英]Group specific rows from multiple files and save each groups of rows in a new excel file with python (pandas, openpyxl)

Can someone please help me to solve the following issue:有人可以帮我解决以下问题:

  • I have multiple excel files, some of them have 3 columns ('Year','Car','Price') and others 5 columns ('Year','Car','Color','Places','Country');我有多个 excel 文件,其中一些有 3 列('Year'、'Car'、'Price'),其他有 5 列('Year'、'Car'、'Color'、'Places'、'Country') ;

  • In a specific column ('Year') of each file, I want to group the rows by year;在每个文件的特定列(“年份”)中,我想按年份对行进行分组;

  • Then I want to save these groups of rows in different sheets of a new file.然后我想将这些行组保存在一个新文件的不同工作表中。

My actual issue is that when python read and group the rows from these files, my code will only save the last file it red.我的实际问题是,当 python 读取这些文件中的行并将其分组时,我的代码只会将最后一个文件保存为红色。

Thanks a lot by advance!非常感谢!

from tkinter import filedialog
import pandas as pd

window = Tk()
window.title("title")
#(etc.)
label .pack()

def action():
     all_files = filedialog.askopenfilename(initialdir = "/", 
     multiple=True,
     title="select",
     filetypes=(
             ("all files", "*.*"),
             ("Excel", "*.xlsx*")))
      dossier=filedialog.askdirectory()
      final=pd.DataFrame()
      first=True
      for f in all_files:
           step1 =pd.read_excel(f,sheet_name=0)
           final=step1
           final['Year']=final['Year'].apply(str)
           lst1=final.groupby('Year')
           lst0=lst1.get_group('2013')
           with pd.ExcelWriter(dossier+'\\sells.xlsx') as writer:
                lst0.to_excel(writer, sheet_name='2013',index=False)
    tkinter.messagebox.showinfo("Files", "Ready")

ExcelWriter has default mode set to write: ExcelWriter的默认模式设置为写入:

mode{'w', 'a'}, default 'w' File mode to use (write or append). mode{'w', 'a'}, 默认 'w'要使用的文件模式(写入或追加)。 Append does not work with fsspec URLs. Append 不适用于 fsspec URL。

Try specifying append mode with if_sheet_exists set to overlay :尝试指定 append 模式并将if_sheet_exists设置为overlay

if_sheet_exists{'error', 'new', 'replace', 'overlay'}, default 'error' if_sheet_exists{'error', 'new', 'replace', 'overlay'}, 默认 'error'
How to behave when trying to write to a sheet that already exists (append mode only).尝试写入已存在的工作表时的行为方式(仅限追加模式)。

  • error: raise a ValueError.错误:引发 ValueError。
  • new: Create a new sheet, with a name determined by the engine. new:创建一个新的sheet,名字由引擎决定。
  • replace: Delete the contents of the sheet before writing to it. replace:在写入之前删除工作表的内容。
  • overlay: Write contents to the existing sheet without removing the old contents.覆盖:将内容写入现有工作表而不删除旧内容。
with pd.ExcelWriter(dossier+'\\sells.xlsx', mode="a", if_sheet_exists="overlay") as writer:
   # ...

暂无
暂无

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

相关问题 从多个 csv 文件中提取所有特定行(单独)并组合行以另存为新文件 - Extracting all specific rows (separately) from multiple csv files and combine rows to save as a new file 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python 如何使用 python 从多个 excel 文件中读取和组合特定行到单个文件中? - How to read & combine specific rows from multiple excel files into a single file using python? 如何在 Python 中使用 Openpyxl 对多行的 excel 行进行平均? - How to average across excel rows for multiple rows using Openpyxl in Python? 将具有不同行的多个Excel文件合并到一个熊猫中的Excel文件中 - Merge multiple Excel files with varied rows into one Excel file in pandas openpyxl在Excel中写多行 - openpyxl write multiple rows in Excel 如果满足条件,如何使用 openpyxl python 删除 excel 中的特定行 - How to delete specific rows in excel with openpyxl python if condition is met 将一个 excel 文件拆分为多个,其中 Pandas 具有特定的行数 - Split one excel file into multiple with specific number of rows in Pandas openpyxl-在具有合并单元格的excel文件中添加新行 - openpyxl - adding new rows in excel file with merged cell existing 有没有办法将 openpyxl 行(存储在列表中)写入新的 Excel 文件? - Is there a way to write openpyxl rows (stored in a list) into a new Excel file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM