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
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With