简体   繁体   中英

Combining corresponding columns between two separate dataframes into new dataframe

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

You could try a more fancy way of ordering here: Pandas concatenate alternating columns

But it would be much easier to read the code if the dataframes were combined explicitly in the desired way.

First declare new column names:

dataCols = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']

Then alternate the Series:

dataSeries = [df1.Column1, df2.Column1, df1.Column2, df2.Column2, df1.Column3, df2.Column3]

(Use df1['Column 1'] if there are spaces in your current column names)

Then combine into a dictionary and create a dataframe:

dataDict = dict(list(zip(dataCols, dataSeries)
newDf = pd.DataFrame(dataDict)

This will create a dataframe with alternating columns.

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM