0

i have several dataframes, with all the same columns, within one list that i would like to have within one dataframe.

For instance, i have these three dataframes here:

df1 = pd.DataFrame(np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]),
                   columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]]),
                   columns=['a', 'b', 'c'])
df3 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

within one list:

dfList = [df1,df2,df3]

I know i can use the following which provides me with exactly what I'm looking for:

df_merge = pd.concat([dfList[0],dfList[1],dfList[2]])

However, my in my actual data i have 100s of dataframes within a list, so I'm trying to find a way to loop through and concat:

dfList_all = pd.DataFrame()
for i in range(len(dfList)):
    dfList_all = pd.concat(dfList[i])

I tried the following above, but it provides me with the following error:

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

Any ideas would be wonderful. Thanks

lapgoch
  • 35
  • 7
  • 2
    Why not `pd.concat(dfList)`? – Shubham Sharma May 01 '22 at 12:08
  • sometimes the answers are so simple :) .. i thought i tried it before and it didnt work, but worked perfectly now haha. Thanks – lapgoch May 01 '22 at 12:43
  • Also, don't ever call `pandas.concat` and soon to be deprecated `DataFrame.append` inside a for-loop. It leads to [quadratic copying](https://stackoverflow.com/a/36489724/1422451). – Parfait May 01 '22 at 13:00
  • Possible duplicates: [Append multiple pandas data frames at once](https://stackoverflow.com/q/36526282/1422451), [Efficient way to combine pandas data frames row-wise](https://stackoverflow.com/q/38246166/1422451), [Append more than 2 data frames in pandas](https://stackoverflow.com/q/34072671/1422451) – Parfait May 01 '22 at 13:12

0 Answers0