0

I have two dataframes like this:

dataframe1
col1      col2
s8771      1
s9562      1
s3352      1

dataframe2
col1      col2
s834      0
s5216     0
s8104     0
s174      0

I want to combine both dataframes, I know pd.appendbut what I want is one row from each dataframe at a time. As an output:

result
col1      col2
s8771      1
s834       0
s9562      1
s5216      0
s3352      1
s8104      0
s174       0

Any help please?

LamaMo
  • 576
  • 2
  • 8
  • 19

2 Answers2

1

You could try with:

result = pd.concat([dataframe1, dataframe2]).sort_index(kind='merge')

Example:

df = pd.DataFrame( {'a':[1,2,3,4,5], 'b':[0,0,0,0,0]})
df1 = pd.DataFrame( {'a':[6,7,8,9], 'b':[1,1,1,1]})
df2 = pd.concat([df, df1]).sort_index(kind='merge')

Output:

   a  b
0  1  0
0  6  1
1  2  0
1  7  1
2  3  0
2  8  1
3  4  0
3  9  1
4  5  0
Joe
  • 12,057
  • 5
  • 39
  • 55
  • Might want to point out the reason you're specifying a mergesort instead of a quicksort here... maybe by quoting the line in the docs that says *mergesort is the only stable algorithm* and why that's important here... – Jon Clements Aug 14 '18 at 07:02
0

Use append + sort_index + reset_index with drop=True for default indices:

df = dataframe1.append(dataframe2).sort_index().reset_index(drop=True)
print (df)
    col1  col2
0  s8771     1
1   s834     0
2  s9562     1
3  s5216     0
4  s3352     1
5  s8104     0
6   s174     0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 5 mins later putting that "mergesort" in than Joe :) – Jon Clements Aug 14 '18 at 07:06
  • Frankly, I've never noticed a difference... I was just pointing out that Joe's answer came straight with "mergesort" 5 minutes before you added it... so now the difference is, is `.append` or `.concat` a better choice to use here... – Jon Clements Aug 14 '18 at 07:11
  • @JonClements - I try some research and seems [`mergesort`](https://stackoverflow.com/a/19615764) is not necessary. – jezrael Aug 14 '18 at 07:23
  • 1
    Yeah... just tried a few things with `df = pd.DataFrame({'value': range(10000)}, index=[0] * 10000)` etc... and it appears sorting a single level index with the default is fine... – Jon Clements Aug 14 '18 at 07:36