Home > Enterprise >  Combining multiple 2D DataFrames index-wise in Pandas
Combining multiple 2D DataFrames index-wise in Pandas

Time:01-29

Let's say I have three different DataFrames (with the matching number of columns and rows, 3x3 in this sample):

DataFrame1   DataFrame2   Dataframe3

A1 A2 A3     B1 B2 B3     C1 C2 C3 
A4 A5 A6     B4 B5 B6     C4 C5 C6
A7 A8 A9     B7 B8 B9     C7 C8 C9

I want to combine them into new DataFrame. Values from each source dataframe should be put into separate column. So the expected result would look like this:

ResultDataFrame

A1 B1 C1
A2 B2 C2
A3 B3 C3
...
A9 B9 C9

How could I achieve that? I looked through samples in Pandas documentation about merging but wasn't able to find case that would match my needs.

My only idea right now is to somehow flatten source DFs into Series, and then prepare new DF -> but I'm quite sure it is not the most clever and efficient way.

CodePudding user response:

Assuming this input:

df = pd.DataFrame(np.arange(1,10).reshape(3,3)).astype(str)
dfs = {x: x df for x in 'ABC'}
{'A':     0   1   2
      0  A1  A2  A3
      1  A4  A5  A6
      2  A7  A8  A9,
 'B':     0   1   2
      0  B1  B2  B3
      1  B4  B5  B6
      2  B7  B8  B9,
 'C':     0   1   2
      0  C1  C2  C3
      1  C4  C5  C6
      2  C7  C8  C9}

You can use pandas.concat and stack:

out = pd.concat(dfs, axis=1).stack(1)

Or if the input is a list l of the dataframes:

pd.concat(dict(enumerate(l)), axis=1).stack(1)

# or

pd.concat(l, keys=['A', 'B', 'C'], axis=1).stack(1))

output:

      A   B   C
0 0  A1  B1  C1
  1  A2  B2  C2
  2  A3  B3  C3
1 0  A4  B4  C4
  1  A5  B5  C5
  2  A6  B6  C6
2 0  A7  B7  C7
  1  A8  B8  C8
  2  A9  B9  C9
  •  Tags:  
  • Related