Reputation: 115
Looking for a way to merge a list of Pandas dataframes for the following effect. Multiple dataframes as shown below, df1 - df3, combined into new_df.
df1:
0 | 1 | 2 | |
---|---|---|---|
Category | A | B | C |
Lunch | 17 | 11 | 6 |
df2:
0 | 1 | 2 | |
---|---|---|---|
Category | A | B | C |
Dinner | 1 | 3 | 5 |
df3:
0 | 1 | 2 | |
---|---|---|---|
Category | A | B | C |
Snacks | 11 | 1 | 6 |
new_df:
0 | 1 | 2 | |
---|---|---|---|
Category | A | B | C |
Lunch | 17 | 11 | 6 |
Dinner | 1 | 3 | 5 |
Snacks | 11 | 1 | 6 |
I have tried:
pdList = [df1,df2,df3]
new_df = pd.concat(pdList)
But it doesnt merge the Category A B C entries as it is concatenation. Thus Category A B C is kept from every dataframe. It is required once, as the figures relate to the category letter above it. Is there a way to use merge on multiple dfs and get this effect?
Upvotes: 0
Views: 33
Reputation: 1628
So your problem here my dude, is that you are trying to concatenate based on your category index. Which is something totally unnecessary since you have it in all of your dfs
Just drop the category column. On your dfs objects, and mantain it in at least one
list_df = [#dfs you want to drop it in]
pdList = []
for i in list_df:
pdList.append(i.drop('Category',axis = 0,inplace = True))
pdList.append(#Df you want to keep the category row)
After this a simple, pd.concat([your_df])
Upvotes: 1