Replace a part of the Worksheet with dataframe

Viewed 19

How should I code for the following condition: if the excel sheet has 100 rows and 20 columns and new dataframe has 30 rows and 5 columns, only 30*5 data should be replaced in the worksheet, rest of the cells should have have old data

1 Answers

You can start by importing the excel file to pandas via pandas.read_excel().

import pandas as pd
excel_data = pd.read_excel(FILE_NAME)

Next, you want to replace the data you want to replace, for example by:

excel_data[0:30][0:5] = new_data.values
# this assumes you have 30 rows and 5 columns of new data

and then save the excel data with:

excel_data.to_excel(FILE_PATH)
Related