[英]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.