Reputation: 49
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.
Upvotes: 0
Views: 154
Reputation: 3103
An easier way is just to do this:
combined_data = pd.concat([pd.read_excel(sheet_name) for sheet_name in data_sheetnames])
Upvotes: 0
Reputation: 833
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
Upvotes: 1