如何使用openpyxl将数据透视表值(列)从熊猫写入Excel?

时间:2019-01-18 20:36:01

标签: python pandas openpyxl

我可以将值写入现有的excel工作表,但是无法使用openpyxl将值从熊猫的数据透视表导出到excel工作表。 以下是我的代码以及我的能力:

import pandas as pd
import openpyxl as op
import numpy as np
from openpyxl import Workbook, worksheet, load_workbook

wb = op.load_workbook("Table1.xlsx")
#ws = wb.active # selects active excel sheet

print(wb.sheetnames) # Shows all available sheet names

ws = wb['Sheet1'] # Select sheet name "Sheet1"
ws['B2'] = 40 # Input on cell B2
ws['B3'] = 18
ws['B4'] = 20
ws['B5'] = 20
ws['B6'] = 20
ws['C2'] = 8 # Input on cell C2
ws['C3'] = 30
ws['C4'] = 4
ws['C5'] = 10
ws['C6'] = 9
ws['D2'] = 89 # Input on cell D2
ws['D3'] = 300
ws['D4'] = 76
ws['D5'] = 20
ws['D6'] = 4

ws1 = wb['agua'] # Select sheet name "agua"
ws1['B2'] = 4 # Input on cell B2
ws1['B3'] = 60
ws1['B4'] = 0
ws1['C2'] = 90
ws1['C3'] = 23
ws1['C4'] = 20

wb.save("test.xlsx") # Saves to new excell worksheet to avoid mistakes

但是我有此数据透视表输出,我需要将数据透视表的每一列填充到要自动填充的现有excel文件表中。看下面:

df2 = pd.read_csv("https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1",encoding="latin-1")

df2['received'] = pd.to_datetime(df2['received'])
df2['sent'] = pd.to_datetime(df2['sent'])

pvt_all = df2.dropna(axis=0, how='all', subset=['received', 'sent'])\
    .pivot_table(index=['site'], values=['received','sent'],\
    aggfunc='count', margins=True, dropna=False)
pvt_all['to_send']= pvt_all['received']-pvt_all['sent'] 
pvt_all=pvt_all[['received','sent','to_send']] 
pvt_all

received    sent    to_send
site            
2   32.0    27.0    5.0
3   20.0    17.0    3.0
4   33.0    31.0    2.0
5   40.0    31.0    9.0
All 125.0   106.0   19.0

完整的数据集在链接中,我不能在此处共享(发布),因为stackoverflow.com将字符限制为30000

我想要在下面写这些列值:

received    sent    to_send
site            
2   32.0    27.0    5.0
3   20.0    17.0    3.0
4   33.0    31.0    2.0
5   40.0    31.0    9.0
All 125.0   106.0   19.0

对于已经具有标题和索引的现有excel工作簿,如下所示:

received    sent    to_send
site            
2       
3       
4   
5   
All 

我为excel工作表提供了更多功能,但我只想了解如何编码以达到所需的结果。

2 个答案:

答案 0 :(得分:0)

简单方法:拥有pvt_all后,只需为其提供一个Excel文件名:

pvt_all.to_excel("filename.xlsx")

有关其他选项,请参见https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

更危险的方式:您已经拥有一个excel,并且想要将此数据框编写为新的表格,请执行以下操作:

import pandas as pd
import openpyxl

excelfilename = "filename.xlsx"
with pd.ExcelWriter(excelfilename, engine="openpyxl") as writer:
    # above: I use openpyxl, you can change this
    writer.book = openpyxl.load_workbook(excelfilename)
    pvt_all.to_excel(writer, "pivot sheet name", index=False)
        # above: index=False to not write dataframe index

更加复杂:您想一次写入一个特定的单元格范围,一次写入一个单元格:

import openpyxl
from openpyxl.utils import get_column_letter

wb = openpyxl.load_workbook(excelfilename)
ws = wb["my sheet"]
row = 3
col = 1
data = pvt_all.values
max_row, max_col = data.shape
for r in range(max_row):
   for c in range(max_col):
       ws[get_column_letter(col+c)+str(row+r)] = data[r][c]
# don't forget to save your workbook after this

答案 1 :(得分:0)

import pandas as pd;
df_excel = pd.read_excel(".\Table1.xlsx"); #Import existing excel template
df_excel.index = df_excel.index + 2 #As we have 2 empty rows in pandas pivot \
#table, we need to start filling on excel row 2 (df_excel.index + 2)

received = pvt_all.received; #reading received column in pivot table
df_excel["received"] = received; #Copying received column from Pandas to received \
#column in excel

sent = pvt_all.sent; #reading sent column in pivot table
df_excel["sent"] = sent; #Copying sent column from Pandas to sent \
#column in excel

to_send = pvt_all.to_send; #reading to_send column in pivot table
df_excel["to_send"] = to_send; #Copying to_send column from Pandas to to_send \
#column in excel

df_excel.to_excel(".\MyNewExcel.xlsx",index=False); #Writing new excel file to \
#avoid mistakes on original excel template.