Merge two data frames per row with the same pandas index

let's say I have the following two data frames X1 and X2. I would like to combine these 2 frames of data in a row so that each index from each dataframe equally combines the corresponding rows from both dataframes.

       A  B C  D
DATE1 a1 b1 c1 d1

DATE2 a2 b2 c2 d2

DATE3 a3 b3 c3 d3


       A B  C  D
DATE1 f1 g1 h1 i1

DATE2 f2 g2 h2 i2

DATE3 f3 g3 h3 i3

how would i combine them to get


      A  B  C  D
DATE1 A1 B1 C1 D1
      f1 g1 h1 i1

DATE2 A2 B2 C2 D2
      f2 g2 h2 i2

DATE3 A3 B3 C3 D3
      f3 g3 h3 i3

I have tried this so far, but this does not work:

 d= pd.concat( { idx : [ X1[idx], X2[idx]]  for idx, value in appended_data1.iterrows() } , axis =1}

thank

+4
source share
2 answers

Option 1

df3 = df1.stack().to_frame('df1')
df3.loc[:, 'df2'] = df2.stack().values
df3 = df3.stack().unstack(1)
df3

enter image description here


Option 2 Generalized

idx = df1.stack().index

dfs = [df1, df2]
dflabels = ['df1', 'df2']

a = np.stack([d.values.flatten() for d in dfs], axis=1)
df3 = pd.DataFrame(a, index=idx, columns=dflabels).stack().unstack(1)

Customization

from StringIO import StringIO
import pandas as pd


df1_text = """       A  B C  D
DATE1 a1 b1 c1 d1
DATE2 a2 b2 c2 d2
DATE3 a3 b3 c3 d3"""


df2_text = """       F  G H  I
DATE1 f1 g1 h1 i1
DATE2 f2 g2 h2 i2
DATE3 f3 g3 h3 i3"""

df1 = pd.read_csv(StringIO(df1_text), delim_whitespace=True)
df2 = pd.read_csv(StringIO(df2_text), delim_whitespace=True)

df1

enter image description here

df2

enter image description here

+6
source

Perhaps this solution may also solve your problem:

df3 =  pd.concat([df1,df2]).sort_index()

print df3
Out[42]: 
         A   B   C   D
DATE1  a1  b1  c1  d1
DATE1  f1  g1  h1  i1
DATE2  a2  b2  c2  d2
DATE2  f2  g2  h2  i2
DATE3  a3  b3  c3  d3
DATE3  f3  g3  h3  i3
+1
source

Source: https://habr.com/ru/post/1653822/


All Articles