Combine pandas data list

There were many similar questions, but there wasn’t specifically for this.

I have a list of data frames and I need to combine them together using a unique column (date). The field names are different, so concat is out.

I can manually use df[0].merge(df[1],on='Date').merge(df[3],on='Date)etc. to combine each df one by one, but the problem is that the number of data frames in the list is different from user input.

Is there a way to merge that simply merges all the data frames in the list at a time? Or maybe some for the loop in this case?

I am using Python 2.7.

+4
source share
1 answer

reduce, dfList - :

import pandas as pd
reduce(lambda x, y: pd.merge(x, y, on = 'Date'), dfList)

:

df = pd.DataFrame({'Date': [1,2,3,4], 'Value': [2,3,3,4]})
dfList = [df, df, df]
dfList

# [   Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4,    Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4,    Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4]

reduce(lambda x, y: pd.merge(x, y, on = 'Date'), dfList)
#   Date  Value_x  Value_y  Value
# 0    1        2        2      2
# 1    2        3        3      3
# 2    3        3        3      3
# 3    4        4        4      4
+19

All Articles