简体   繁体   English

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

[英]How to write pivot table values (columns) from pandas to excel using openpyxl?

I can write values to an existing excel worksheet but I am unable to export values from pivot table on pandas to excel sheet using openpyxl. 我可以将值写入现有的Excel工作表,但是无法使用openpyxl将值从熊猫的数据透视表导出到Excel工作表。 Below are my code and what I am capable: 以下是我的代码以及我的能力:

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

But I have this pivot table output that I need to fill each column of pivot table to that existing excel file sheet to be filled automatically. 但是我有此数据透视表输出,我需要将数据透视表的每一列填充到该现有的excel文件中,以便自动填充。 Look below: 往下看:

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

Full dataset is in the link, I cant share (post) here because stackoverflow.com limits characters to 30000 完整的数据集在链接中,我不能在此处共享(发布),因为stackoverflow.com将字符限制为30000

What I want is to write these column values below: 我想要的是在下面编写这些列值:

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

To an existing excel workbook already with headers and index like below: 到已经具有标题和索引的现有excel工作簿,如下所示:

received    sent    to_send
site            
2       
3       
4   
5   
All 

I have more features for excel sheet but I just want to understand how to code to achieve desired result. 我为excel工作表提供了更多功能,但我只想了解如何编写代码以实现所需的结果。

Simple way: After you have pvt_all , simply give a Excel filename to it: 简单方法:拥有pvt_all ,只需pvt_all提供一个Excel文件名:

pvt_all.to_excel("filename.xlsx")

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

Harder way: You already have an excel and want to write this dataframe as a new sheet, do like this: 困难的方法:您已经拥有一个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

Even more complicated: You want to write to a particular cell range, one cell at a time: 更复杂的是:您想一次写入一个特定的单元格范围,一次写入一个单元格:

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
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.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM