Python Forum

Full Version: Moving data from one Excel to another and finding maximum profit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code: -

import pandas as pd

out = pd.read_excel(io = "E:/Folder/Company Financials.xlsx", sheet_name = "Sales", usecols = "A:J", skiprows = 5)
print(out)
1. How do I change the code so that the data is copied over to a new Excel file but being retained in the original file?
2. How do I find the maximum profit in column H called 'Profit'?

Thanks in advance.
Do you mean a new sheet?
Copy and transfer data from the 'Sales' sheet in "E:/Folder/Company Financials.xlsx" and paste it into a separate new Excel file, say "E:/Folder/Company Financials2.xlsx". The sheet name can be 'Sheet1'.
untested:
import pandas as pd
 

df = pd.read_excel(io = "E:/Folder/Company Financials.xlsx", sheet_name = "Sales", usecols = "A:J", skiprows = 5)
print(df)

with pd.ExcelWriter("E:/Folder/Company Financials2.xlsx") as writer:
    df.to_excel(writer, sheet_name='Sheet1')
Fantastic, thanks.
Is there a way to add a header after the new file is created or include it with the data except rows 2-5?
headers should be added as the new file is being created, as the first record written.
OK I understand. Will try and see how to do that. You've got me thinking Smile