2

I want to concatenate multiple dataframes into one dataframe. The manner I want the concatenation to happen is illustrated in the following example:

Input tables:
   A  B  C  D
0  x  p  2  4
1  y  q  3  5

   A  B  E  F
0  x  p  6  8
1  y  q  9  10

Output table:

   A  B  C  D  E  F
0  x  p  2  4  6  8
1  y  q  3  5  9  10

I want to know if this can be done using the pandas.concat command. I know this can be done through the pd.merge command.
Thanks.

Hrithik Diwakar
  • 119
  • 2
  • 11
  • Yes, it is possible by `pd.concat([df1.set_index(['A','B']), df2.set_index(['A','B'])])` – jezrael Jun 14 '18 at 05:44
  • The Pandas website has great documentation for the pandas.concat function. The pandas.concat() page in particular has several examples using pandas.concat. – James Jun 14 '18 at 05:45
  • @David i read that page, it is very well documented but i couldn't find how to do this using pd.concat, they did it for pd.merge. – Hrithik Diwakar Jun 14 '18 at 05:47
  • https://pandas.pydata.org/pandas-docs/stable/merging.html – Sid Jun 14 '18 at 05:49

1 Answers1

3

Use set_index with list comprehension for MultiIndex and then concat:

dfs = [df1, df2, df3]

df = pd.concat([x.set_index(['A','B']) for x in dfs], axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252