To add a pandas dataframe to an existing excel worksheet that already contains data, you can use the "pandas ExcelWriter" module.
Here are the steps to follow:
import pandas as pd
from openpyxl import load_workbook
# set the file path
file_path = "path/to/your/excel/file.xlsx"
# load the excel workbook
book = load_workbook(file_path)
# define the sheet name where you want to add the dataframe
writer = pd.ExcelWriter(file_path, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df = pd.read_csv('path/to/your/csvfile.csv')
df.to_excel(writer, sheet_name='Sheet1', startrow=3, startcol=1)
writer.save()
Note: In this example, we are adding the dataframe to Sheet1, starting from row 3 and column 1. You can change these values as per your requirement. The key point is to load the existing workbook and pass it to the pandas ExcelWriter function.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-04-29 12:53:16 +0000
Seen: 9 times
Last updated: Apr 29 '23
How can one obtain live data from interactive broker through API?
What is the method to implement pagination for Firestore data in Flutter using ListView?
What is the reason for the submitted Django form's value being null in the database?
How can we create summary tables by using nested tibbles?
What is the process of moving information from one tab to another in Excel by transposing it?
What does "coxphw undefined columns selected" mean?
Is it not possible to change the data type of an array from 'O' to 'float64'?
How to perform a historical backfill from GA4 into BigQuery?
How can Redux be integrated with Ag-Grid's server-side row model in React?