Python Forum
Moving data from one Excel to another and finding maximum profit - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Moving data from one Excel to another and finding maximum profit (/thread-38372.html)



Moving data from one Excel to another and finding maximum profit - azizrasul - Oct-04-2022

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.


RE: Moving data from one Excel to another and finding maximum profit - Larz60+ - Oct-04-2022

Do you mean a new sheet?


RE: Moving data from one Excel to another and finding maximum profit - azizrasul - Oct-04-2022

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


RE: Moving data from one Excel to another and finding maximum profit - Larz60+ - Oct-05-2022

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')



RE: Moving data from one Excel to another and finding maximum profit - azizrasul - Oct-05-2022

Fantastic, thanks.


RE: Moving data from one Excel to another and finding maximum profit - azizrasul - Oct-06-2022

Is there a way to add a header after the new file is created or include it with the data except rows 2-5?


RE: Moving data from one Excel to another and finding maximum profit - Larz60+ - Oct-06-2022

headers should be added as the new file is being created, as the first record written.


RE: Moving data from one Excel to another and finding maximum profit - azizrasul - Oct-06-2022

OK I understand. Will try and see how to do that. You've got me thinking Smile