0

I have two data frames with the same column names and the same indices. Each entry in the data frames is an int or a float. I would like to combine the data frames into a single data frame. I would like each entry of this data frame to be a list containing the individual elements from the separate data frames.

As an example, df1 and df2 are the original data frames:

         A  B
df1 = 0  0  1

         A  B
df2 = 0  2  3

I would like to produce the following dataframe:

            A       B
df3 = 0  [0, 2]  [1, 3]

I tried the following:

merger = lambda s1, s2: s1.append(s2)
df1.combine(df2, merger)

This gives me the error:

ValueError: cannot reindex from a duplicate axis

I can think of a few ways to do it with loops but I'd like to avoid that if possible. It seems like this is something that should be built into pandas.

Cheers

Sal
  • 41
  • 3

1 Answers1

0

Try with

out = pd.concat([df1,df2]).groupby(level=0).agg(list)
BENY
  • 317,841
  • 20
  • 164
  • 234