通过按列拆分 DataFrame 来保存现有文件而不删除现有数据
Save the existing files without deleting existing data by splitting the DataFrame by column
我有三个 excel 文件- A1/A2/A3 中有一些现有数据 'Sheet1'。
import glob
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
df=pd.DataFrame()
for f in glob.glob(r'...\Excel\A*.xlsx'):
info=pd.read_excel(f)
df=df.append(info)
通过上面的代码,我得到了下面的 DataFrame:
Sample Description
----------------------
A1 Auto
A2 Manual
A3 Fully-Automated
我希望 'Sheet2' 中的 A1 数据粘贴到 A1 文件中,A2 数据粘贴到 A2 文件中,A3 数据粘贴到 A3 文件中而不删除现有的 'Sheet1'。
Sample Description
---------------------- ````` this data should go in A1 File
A1 Auto
Sample Description
---------------------- ````` this data should go in A2 File
A2 Manual
Sample Description
---------------------- ````` this data should go in A3 File
A3 Fully-Automated
我尝试编写下面的代码,但只有最后一行被粘贴到所有三个 excel 文件中。
- File A1
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
- File A2
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
- File A3
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
for filename in glob.glob(r'...\Excel\A*.xlsx'):
for name,data in group_df:
book=load_workbook(filename)
writer=pd.ExcelWriter(filename,engine='openpyxl')
writer.book=book
data.to_excel(writer,sheet_name='Sheet2')
writer.save()
writer.close()
我需要按列 'Sample' 对 DataFrame 进行分组,然后将拆分后的数据粘贴回相应的文件,并使用 New sheet as 'Sheet2' 而不删除现有的 'Sheet1'。
如果没有最低限度的可重现示例,很难确定,但这应该会引导您找到解决方案;
import glob
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
# this makes things a bit easier
folder_path = r'...\Excel\'
# that's ok
df=pd.DataFrame()
for f in glob.glob(folder_path + 'A*.xlsx'):
info=pd.read_excel(f)
df=df.append(info)
# iterate over the groupby
# name is the label in the column 'Sample'
# group is a dataframe
for name, group in df.groupby('Sample'):
# file path for the name of the group (A1, A2, ...)
filename = folder_path + name + '.xls'
# do your thing
book=load_workbook(filename)
writer=pd.ExcelWriter(filename,engine='openpyxl')
writer.book=book
# save the group to excel
group.to_excel(writer,sheet_name='Sheet2')
writer.save()
writer.close()
我有三个 excel 文件- A1/A2/A3 中有一些现有数据 'Sheet1'。
import glob
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
df=pd.DataFrame()
for f in glob.glob(r'...\Excel\A*.xlsx'):
info=pd.read_excel(f)
df=df.append(info)
通过上面的代码,我得到了下面的 DataFrame:
Sample Description
----------------------
A1 Auto
A2 Manual
A3 Fully-Automated
我希望 'Sheet2' 中的 A1 数据粘贴到 A1 文件中,A2 数据粘贴到 A2 文件中,A3 数据粘贴到 A3 文件中而不删除现有的 'Sheet1'。
Sample Description
---------------------- ````` this data should go in A1 File
A1 Auto
Sample Description
---------------------- ````` this data should go in A2 File
A2 Manual
Sample Description
---------------------- ````` this data should go in A3 File
A3 Fully-Automated
我尝试编写下面的代码,但只有最后一行被粘贴到所有三个 excel 文件中。
- File A1
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
- File A2
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
- File A3
*Sheet2*
Sample Description
----------------------
A3 Fully-Automated
for filename in glob.glob(r'...\Excel\A*.xlsx'):
for name,data in group_df:
book=load_workbook(filename)
writer=pd.ExcelWriter(filename,engine='openpyxl')
writer.book=book
data.to_excel(writer,sheet_name='Sheet2')
writer.save()
writer.close()
我需要按列 'Sample' 对 DataFrame 进行分组,然后将拆分后的数据粘贴回相应的文件,并使用 New sheet as 'Sheet2' 而不删除现有的 'Sheet1'。
如果没有最低限度的可重现示例,很难确定,但这应该会引导您找到解决方案;
import glob
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
# this makes things a bit easier
folder_path = r'...\Excel\'
# that's ok
df=pd.DataFrame()
for f in glob.glob(folder_path + 'A*.xlsx'):
info=pd.read_excel(f)
df=df.append(info)
# iterate over the groupby
# name is the label in the column 'Sample'
# group is a dataframe
for name, group in df.groupby('Sample'):
# file path for the name of the group (A1, A2, ...)
filename = folder_path + name + '.xls'
# do your thing
book=load_workbook(filename)
writer=pd.ExcelWriter(filename,engine='openpyxl')
writer.book=book
# save the group to excel
group.to_excel(writer,sheet_name='Sheet2')
writer.save()
writer.close()