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.

  • Possible duplicate of [Pandas/Python: How to concatenate two dataframes without duplicates?](https://stackoverflow.com/questions/21317384/pandas-python-how-to-concatenate-two-dataframes-without-duplicates) – jar Oct 17 '18 at 11:38

2 Answers2

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

DiCaprio
  • 823
  • 1
  • 7
  • 24
0

An easier way is just to do this:

combined_data = pd.concat([pd.read_excel(sheet_name) for sheet_name in data_sheetnames])
iDrwish
  • 3,085
  • 1
  • 15
  • 24