0

I have two dataframes looking like below:

df1

Column 1   Column 2  Column 3 
0.2         0.4       0.5 
0.25        0.44      0.45 
0.26        0.32      0.33

df2

Column 1   Column 2  Column 3 
340         350       360
410         400       350
234         324       450

How can I combine df1 and df2 to make a dataframe df3 that has Columns of the same # side-by-side, i.e.

df3 
Column 1  Column 1  Column 2  Column 2  Column 3  Column 3
0.2         340       0.4       350      0.5       360
0.25        410       0.44      400      0.45      350 
0.26        234       0.32      324      0.33      450

Thank you!

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Jan Rostenkowski
  • 315
  • 2
  • 11
  • Something like `pd.concat([df1, df2], axis=1).sort_index(axis=1)` – cs95 Jul 17 '19 at 23:23
  • This makes the number of rows twice as large, and so half the elements in each column are N/A – Jan Rostenkowski Jul 17 '19 at 23:46
  • 1
    It can happen if your indexes are not the same. What if you do `df1.index = df2.index` before calling `concat`? – cs95 Jul 17 '19 at 23:46
  • another option (thought not as classic as concat) pd.merge(df1, df2, left_index=True, right_index=True, how='outer') – adhg Jul 18 '19 at 00:10

2 Answers2

1

To alternate the columns for any dataframe (with any, possibly non-identical column names), first combine the two dataframes and then reorder them by passing a list of column names in the order that you want.

For alternating order, first get lists of the two dataframe column names

l1 = df1.columns
l2 = df2.columns

Then create pairs of column names zipping them the two lists (results in ('col1','col1').... ect.)

colNames = zip(l1, l2)

Then combine in an alternating fashion with list comprehension

combinedNames = [name for pair in colNames for name in pair]

This will create a list with paired columns names.

Apply this list to your combined dataframe to reorder it:

combinedDf = combinedDf[combinedNames]
johnDanger
  • 1,990
  • 16
  • 22
0

A simpler way to do that is by defining a new DataFrame and using the pandas.DataFrame.append() function in a for loop so that you alternate the 2 DataFrames. Then you need to transform your new DataFrame to get it right:

NumColumn=d1.shape[1]

NewDF = pd.DataFrame()
for i in range(NumColumn):
  NewDF = NewDF.append(d1.iloc[:, i])
  NewDF = NewDF.append(d2.iloc[:, i])


NewDF = NewDF.T
RandML000
  • 11
  • 1
  • 4