0

I have multiple sheets that are identical in column headers but not in terms of the number of rows. I want to combine the sheets to make one master sheet.

At the moment this is the code that I get, for which the output is blank and I end up with data = to that in the last sheet.

I decided to utilise a for loop iterated through data_sheetnames which is a list.

Below is the code I have utilised

combineddata = pd.DataFrame()
for club in data_sheetnames:
    data = pd.read_excel(r'''C:\Users\me\Desktop\Data.xlsx''', header = 1, index_col = 2, sheet_name = club)
    combineddata.append(data)

If I were to change combineddata to a blank list then I get a dictionary of dataframes.

1

2 Answers 2

1

The solution is that append does not work in place. It returns the appended DataFrame. Therefore

combineddata = pd.DataFrame()
for club in data_sheetnames:
    data = pd.read_excel(r'''C:\Users\me\Desktop\Data.xlsx''', header = 1, index_col = 2, sheet_name = club)
    combineddata = combineddata.append(data)

should solve the issue

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

1 Comment

Thanks, this has solved the query. I can only accept in 5 minutes.
0

An easier way is just to do this:

combined_data = pd.concat([pd.read_excel(sheet_name) for sheet_name in data_sheetnames])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.