Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two dataframes with different indices while preserving the main dataframe's index using a one-line code

Tags:

I have two dataframes; the first one(df1) is:

df1 = pd.DataFrame({'col1': [0,1], 'col2': [0,1]})
df1 = df1.rename(index = {k:v for k,v in zip([0,1],['zero','one'])})
print(df1)
      col1  col2
zero    0   0
one     1   1

and the second one(df2) is:

df2 = pd.DataFrame({k:v for k,v in zip(['col'+str(i) for i in range(3,10)],
                            [[2,3]]*7)
                            })
print(df2)
   col3  col4  col5  col6  col7  col8  col9
0     2     2     2     2     2     2     2
1     3     3     3     3     3     3     3

the final product(df3) should look exactly like:

      col1  col2  col3  col4  col5  col6  col7  col8  col9
zero     0     0     2     2     2     2     2     2     2
one      1     1     3     3     3     3     3     3     3

This is the way I do it, which is a bit unpythonic to my taste:

df3 = df1.reset_index(drop = True)
df3 = df3.join(df2.reset_index(drop = True))
df3 = df3.rename(index = {k:v for k,v in zip(df3.index,df1.index)})
print(df3)

Is there any one-line code that can do the job? Thank you guys

like image 778
mathguy Avatar asked Dec 13 '18 13:12

mathguy


1 Answers

You can create df2.index by df1.index by set_index, only necessary same length of both DataFrames:

df = df1.join(df2.set_index(df1.index))

Or:

df = pd.concat([df1, df2.set_index(df1.index)], axis=1)
print (df)
      col1  col2  col3  col4  col5  col6  col7  col8  col9
zero     0     0     2     2     2     2     2     2     2
one      1     1     3     3     3     3     3     3     3

If have list same length like both DataFrames pass nested list for distinguish you want pass list, not list of column names (df2.set_index(L) or df2.set_index(['a','b'])):

L = ['a','b']
df = pd.concat([df1.set_index([L]), df2.set_index([L])], axis=1)
print (df)
   col1  col2  col3  col4  col5  col6  col7  col8  col9
a     0     0     2     2     2     2     2     2     2
b     1     1     3     3     3     3     3     3     3
like image 159
jezrael Avatar answered Oct 12 '22 12:10

jezrael