Home > Net >  Combine multiple functions into one in python
Combine multiple functions into one in python

Time:10-26

I made 3 functions to import data from a .csv file and then take a specific column to then sum the data within the column. I want to export all three dataFrames from each respective function into one .xlsx file. If I run the functions one by one, they will overlap and only the last function will be exported to the .xlsx file. How do I export all three functions simultaneously so that the .xlsx file will have all three of the exports? P.S this is my second week of python, this might be a very amateur-like question. thanks.

 import pandas as pd


def file1(file_name_one):
    importing = pd.read_csv(file_name_one)
    df1 = pd.DataFrame(data={'Net Sales':[importing['Net Sales'].sum()]})
      df1.to_excel('excelfile.xlsx',
             index=False,
             header=False,
             startrow=5,
             startcol=3,
             )

def file2(file_name_two):
    importing2 = pd.read_csv(file_name_two)
    df2 = pd.DataFrame(data={'Net Sales':[importing2['Net Sales'].sum()]})
      df2.to_excel('excelfile.xlsx',
             index=False,
             header=False,
             startrow=6,
             startcol=3,
             )

def file3(file_name_three):
    importing3 = pd.read_csv(file_name_three)
    df3 = pd.DataFrame(data={'Amount':[importing3['Amount'].sum()]})
      df3.to_excel('excelfile.xlsx',
             index=False,
             header=False,
             startrow=7,
             startcol=3,
             )

print(file1(r"example1.csv"))
print(file2(r"example2.csv"))
print(file3(r"example3.csv"))

#this will result only in the export of file3(), 
#the other 2 functions get "overlapped", how do I also export the other two functions? 

CodePudding user response:

Based on what I understand, you want the 3 data frames to be stored in the same excel file.

The issue with your code is you're over-writing the data in the same sheet. Instead you can save each dataframe in its won sheet like this:

df1.to_excel('excelfile.xlsx', sheet_name='df1' ...

df1.to_excel('excelfile.xlsx', sheet_name='df1' ...

df3.to_excel('excelfile.xlsx', sheet_name='df3' ...

CodePudding user response:

So in theory:

def handle_files(file_net1, file_net2, file_amount):
    net1 = pd.read_csv(file_net1)
    net2 = pd.read_csv(file_net2)
    amount = pd.read_csv(file_amount)
    df1 = pd.DataFrame(data={'Net Sales':[net1['Net Sales'].sum()]})
    df2 = pd.DataFrame(data={'Net Sales':[net2['Net Sales'].sum()]})
    df3 = pd.DataFrame(data={'Amount':[amount['Amount'].sum()]})
    df1.to_excel('excelfile.xlsx',
             sheet=NetResult1
             index=False,
             header=False,
             startrow=7,
             startcol=3,
             )
    df2.to_excel('excelfile.xlsx',
             sheet=NetResult2
             index=False,
             header=False,
             startrow=7,
             startcol=3,
             )
    df3.to_excel('excelfile.xlsx',
             sheet=Amount
             index=False,
             header=False,
             startrow=7,
             startcol=3,
             )

Should do the trick right?

Ow and the call should be:

handle_files(example1.csv, example2.csv, example3.csv)

BTW: I included @Gangula's anwers in here as well, with the different sheets.

  • Related