1

I am trying to make a reusable functions in python that would read two Excel files and save save to another.

My function looks like this:

def excel_reader(df1, file1, sheet1, df2, file2, sheet2):
    df1 = pd.read_excel(file1, sheet1)
    df2 = pd.read_excel(file2, sheet2)

def save_to_excel(df1, filename1, sheet1, df2, filename2, sheet2):
    df1.to_excel(filename1, sheet1)
    df2.to_excel(filename2, sheet2)

I am calling to the functions as:

excel_reader(df1, 'some_file1.xlsx', 'sheet_name1', 
                df2, 'some_file2.xlsx', 'sheet_name2')

save_to_excel(df1, 'some_file1.xlsx', 'sheet_name1', 
                        df2, 'some_file2.xlsx', 'sheet_name2')

It do not have any errors but it do not create the Excel files that should be performed by save_to_excel function.

It reads the function parameters until df2 parameter and returns error for the last two.

I will be using pd.read_excel() quite a number of times in my code so I am trying to make it a function. I am also aware the the read_excel() reads the filenames as string and tried doing `somefile.xlsx' but still the same result.

The Excel files that will be read are on the same path of the python script.

Question: Any advice on how this would work? Is it advisable to make this a function or should I just use read_excel() repetitively?

6
  • 1
    Can you show the line where you are trying to call the function? Commented May 10, 2018 at 15:06
  • @splinter Hi, I updated my question. as for the first one, I really lack arguments when I called my functions. Now that I added them, it doesn't return anything. Commented May 10, 2018 at 15:18
  • do you want the dunction ro returhn the two Dataframes separately, or concatenate them into one big dataframe? Commented May 10, 2018 at 15:19
  • I would just want to return the two Dataframes separately. Commented May 10, 2018 at 15:21
  • I see, see answer below. I think this is what you are trying to do Commented May 10, 2018 at 15:26

2 Answers 2

2

I don't think this function would improve anything...

Just imagine that you have to call pd.read_excel() with different parameters for different Excel files - for example:

df1 = pd.read_excel(file1, 'Sheet1', skiprows=1)
df2 = pd.read_excel(file2, 'Sheet2', usecols="A,C,E:F")

You will loose all this flexibility using your custom function...

Sign up to request clarification or add additional context in comments.

2 Comments

I understand that. But there are quite a number of times that I will only be using:¶ df1 = pd.read_excel(file1, sheet1) df2 = pd.read_excel(file2, sheet2)
@RickyAguilar, I think you should give us a bigger picture... What are you doing with those pairs of excel files? We can give you better answers if we will know bit more...
1

You are missing the return call. If you just want the function to return the dataframes then

def excel_reader(file1, sheet1, file2, sheet2):
    df1 = pd.read_excel(file1, sheet1)
    df2 = pd.read_excel(file2, sheet2)
    return df1, df2

df1, df2 = excel_reader('some_file1.xlsx', 'sheet_name1', 'some_file2.xlsx', 'sheet_name2')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.