如何使用Pandas在现有的Excel文件中保存新工作表?

时间:2017-02-21 15:07:05

标签: python pandas openpyxl xlsxwriter

我想使用excel文件存储用python详细说明的数据。我的问题是我无法将图纸添加到现有的Excel文件中。在这里,我建议使用示例代码来解决此问题

import pandas as pd
import numpy as np

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"

x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)

x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)

writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df1.to_excel(writer, sheet_name = 'x1')
df2.to_excel(writer, sheet_name = 'x2')
writer.save()
writer.close()

此代码将两个DataFrame保存为两个表,分别名为“x1”和“x2”。如果我创建两个新的DataFrame并尝试使用相同的代码添加两个新工作表'x3'和'x4',原始数据将丢失。

import pandas as pd
import numpy as np

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"

x3 = np.random.randn(100, 2)
df3 = pd.DataFrame(x3)

x4 = np.random.randn(100, 2)
df4 = pd.DataFrame(x4)

writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df3.to_excel(writer, sheet_name = 'x3')
df4.to_excel(writer, sheet_name = 'x4')
writer.save()
writer.close()

我想要一个包含四张纸的excel文件:'x1','x2','x3','x4'。 我知道'xlsxwriter'不是唯一的“引擎”,有'openpyxl'。我也看到已经有其他人写过关于这个问题的文章,但我仍然无法理解如何做到这一点。

此处的代码取自此link

import pandas
from openpyxl import load_workbook

book = load_workbook('Masterfile.xlsx')
writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])

writer.save()

他们说它有效,但很难弄清楚如何。我不明白在这种情况下“ws.title”,“ws”和“dict”是什么。

哪种方法可以保存“x1”和“x2”,然后关闭文件,再次打开并添加“x3”和“x4”?

10 个答案:

答案 0 :(得分:45)

谢谢。我相信一个完整的例子可能对其他有同样问题的人有好处:

import pandas as pd
import numpy as np

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"

x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)

x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)

writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df1.to_excel(writer, sheet_name = 'x1')
df2.to_excel(writer, sheet_name = 'x2')
writer.save()
writer.close()

在这里我生成一个excel文件,根据我的理解,它是通过“xslxwriter”还是“openpyxl”引擎生成并不重要。

当我想写而不丢失原始数据时

import pandas as pd
import numpy as np
from openpyxl import load_workbook

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"

book = load_workbook(path)
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book

x3 = np.random.randn(100, 2)
df3 = pd.DataFrame(x3)

x4 = np.random.randn(100, 2)
df4 = pd.DataFrame(x4)

df3.to_excel(writer, sheet_name = 'x3')
df4.to_excel(writer, sheet_name = 'x4')
writer.save()
writer.close()

这段代码完成了工作!

答案 1 :(得分:13)

在您分享的示例中,您将现有文件加载到book并将writer.book值设置为book。在第writer.sheets = dict((ws.title, ws) for ws in book.worksheets)行中,您正在访问工作簿中的每个工作表ws。然后,工作表标题为ws,因此您要创建{sheet_titles: sheet}键,值对的字典。然后将此字典设置为writer.sheets。 基本上这些步骤只是加载来自'Masterfile.xlsx'的现有数据,并用它们填充您的作者。

现在假设您已经有一个包含x1x2的文件作为工作表。您可以使用示例代码加载文件,然后可以执行类似的操作来添加x3x4

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"
writer = pd.ExcelWriter(path, engine='openpyxl')
df3.to_excel(writer, 'x3', index=False)
df4.to_excel(writer, 'x4', index=False)
writer.save()

那应该做你想要的。

答案 2 :(得分:8)

一次将多个数据写入excel的简单示例。当您想要将数据附加到已写入的Excel文件(已关闭的Excel文件)上的工作表时。

当你第一次写信给excel时。 (写" df1"和" df2"到" 1st_sheet"和" 2nd_sheet")

import pandas as pd 
from openpyxl import load_workbook

df1 = pd.DataFrame([[1],[1]], columns=['a'])
df2 = pd.DataFrame([[2],[2]], columns=['b'])
df3 = pd.DataFrame([[3],[3]], columns=['c'])

excel_dir = "my/excel/dir"

with pd.ExcelWriter(excel_dir, engine='xlsxwriter') as writer:    
    df1.to_excel(writer, '1st_sheet')   
    df2.to_excel(writer, '2nd_sheet')   
    writer.save()    

关闭excel之后,您希望"追加"同一个excel文件上的数据,但另一张表,让我们说" df3"到工作表名称" 3rd_sheet"。

book = load_workbook(excel_dir)
with pd.ExcelWriter(excel_dir, engine='openpyxl') as writer:
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)    

    ## Your dataframe to append. 
    df3.to_excel(writer, '3rd_sheet')  

    writer.save()     

请注意,excel格式不能是xls,您可以使用xlsx。

答案 3 :(得分:7)

我强烈建议您直接与openpyxl since it now supports Pandas DataFrames合作。

这使您可以专注于相关的Excel和Pandas代码。

答案 4 :(得分:4)

用于创建新文件

x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)
with pd.ExcelWriter('sample.xlsx') as writer:  
    df1.to_excel(writer, sheet_name='x1')

要附加到文件,请在mode='a'中使用参数pd.ExcelWriter

x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)
with pd.ExcelWriter('sample.xlsx', engine='openpyxl', mode='a') as writer:  
    df2.to_excel(writer, sheet_name='x2')

默认值为mode ='w'。 参见documentation

答案 5 :(得分:3)

使用openpyxl中的工具无需使用ExcelWriter即可完成此操作 使用openpyxl.styles

可以使添加字体到新工作表更加容易。
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

#Location of original excel sheet
fileLocation =r'C:\workspace\data.xlsx'

#Location of new file which can be the same as original file
writeLocation=r'C:\workspace\dataNew.xlsx'

data = {'Name':['Tom','Paul','Jeremy'],'Age':[32,43,34],'Salary':[20000,34000,32000]}

#The dataframe you want to add
df = pd.DataFrame(data)

#Load existing sheet as it is
book = load_workbook(fileLocation)
#create a new sheet
sheet = book.create_sheet("Sheet Name")

#Load dataframe into new sheet
for row in dataframe_to_rows(df, index=False, header=True):
    sheet.append(row)

#Save the modified excel at desired location    
book.save(writeLocation)

答案 6 :(得分:0)

您可以将感兴趣的现有工作表(例如“ x1”,“ x2”)读入内存,然后在添加更多新工作表之前将其“写回”(请注意,文件中的工作表和内存中的工作表是两种不同的东西,如果您不阅读它们,它们将会丢失)。这种方法仅使用“ xlsxwriter”,不涉及openpyxl。

import pandas as pd
import numpy as np

path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx"

# begin <== read selected sheets and write them back
df1 = pd.read_excel(path, sheet_name='x1', index_col=0) # or sheet_name=0
df2 = pd.read_excel(path, sheet_name='x2', index_col=0) # or sheet_name=1
writer = pd.ExcelWriter(path, engine='xlsxwriter')
df1.to_excel(writer, sheet_name='x1')
df2.to_excel(writer, sheet_name='x2')
# end ==>

# now create more new sheets
x3 = np.random.randn(100, 2)
df3 = pd.DataFrame(x3)

x4 = np.random.randn(100, 2)
df4 = pd.DataFrame(x4)

df3.to_excel(writer, sheet_name='x3')
df4.to_excel(writer, sheet_name='x4')
writer.save()
writer.close()

如果要保留所有现有工作表,则可以在开头和结尾之间用以下代码替换上面的代码:

# read all existing sheets and write them back
writer = pd.ExcelWriter(path, engine='xlsxwriter')
xlsx = pd.ExcelFile(path)
for sheet in xlsx.sheet_names:
    df = xlsx.parse(sheet_name=sheet, index_col=0)
    df.to_excel(writer, sheet_name=sheet)

答案 7 :(得分:0)

#This program is to read from excel workbook to fetch only the URL domain names and write to the existing excel workbook in a different sheet..
#Developer - Nilesh K
import pandas as pd
from openpyxl import load_workbook #for writting to the existing workbook

df = pd.read_excel("urlsearch_test.xlsx")

#You can use the below for the relative path.
# r"C:\Users\xyz\Desktop\Python\

l = [] #To make a list in for loop

#begin
#loop starts here for fetching http from a string and iterate thru the entire sheet. You can have your own logic here.
for index, row in df.iterrows():
    try: 
        str = (row['TEXT']) #string to read and iterate
        y = (index)
        str_pos = str.index('http') #fetched the index position for http
        str_pos1 = str.index('/', str.index('/')+2) #fetched the second 3rd position of / starting from http
        str_op = str[str_pos:str_pos1] #Substring the domain name
        l.append(str_op) #append the list with domain names

    #Error handling to skip the error rows and continue.
    except ValueError:
            print('Error!')
print(l)
l = list(dict.fromkeys(l)) #Keep distinct values, you can comment this line to get all the values
df1 = pd.DataFrame(l,columns=['URL']) #Create dataframe using the list
#end

#Write using openpyxl so it can be written to same workbook
book = load_workbook('urlsearch_test.xlsx')
writer = pd.ExcelWriter('urlsearch_test.xlsx',engine = 'openpyxl')
writer.book = book
df1.to_excel(writer,sheet_name = 'Sheet3')
writer.save()
writer.close()

#The below can be used to write to a different workbook without using openpyxl
#df1.to_excel(r"C:\Users\xyz\Desktop\Python\urlsearch1_test.xlsx",index='false',sheet_name='sheet1')

答案 8 :(得分:0)

另一种比较简单的方法是制作这样的方法:

def _write_frame_to_new_sheet(path_to_file=None, sheet_name='sheet', data_frame=None):
    book = None
    try:
        book = load_workbook(path_to_file)
    except Exception:
        logging.debug('Creating new workbook at %s', path_to_file)
    with pd.ExcelWriter(path_to_file, engine='openpyxl') as writer:
        if book is not None:
            writer.book = book
        data_frame.to_excel(writer, sheet_name, index=False)

这里的想法是将工作簿加载到 path_to_file (如果存在),然后将 data_frame 作为新工作表附加到 sheet_name 。如果工作簿不存在,则会创建它。似乎都没有 openpyxl xlsxwriter 附加,因此,如上面@Stefano的示例所示,您确实必须加载然后重写以附加。

答案 9 :(得分:0)

import pandas as pd
import openpyxl

writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
data_df.to_excel(writer, 'sheet_name')
writer.save()
writer.close()