Python: create a single dataframe, otherwise include lines from the other two

Suppose I have two pandas views:

>>> df
                A          B          C
first   62.184209  39.414005  60.716563
second  51.508214  94.354199  16.938342
third   36.081861  39.440953  38.088336
>>> df1
               A         B         C
first   0.828069  0.762570  0.717368
second  0.136098  0.991668  0.547499
third   0.120465  0.546807  0.346949
>>>

      

What I created with:

  import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.random([3, 3])*100,
columns=['A', 'B', 'C'], index=['first', 'second', 'third'])

df1 = pd.DataFrame(np.random.random([3, 3]),
columns=['A', 'B', 'C'], index=['first', 'second', 'third'])

      

Could you please find the smartest and fastest way to get something like:

                A          B          C
first   62.184209  39.414005  60.716563
first_s   0.828069  0.762570  0.717368
second  51.508214  94.354199  16.938342
second_s  0.136098  0.991668  0.547499
third   36.081861  39.440953  38.088336
third_s   0.120465  0.546807  0.346949

      

?

I think I could do with a loop saying I am taking even lines from the first and odd lines from the second, but that doesn't seem very efficient to me.

+3


source to share


1 answer


Try the following:



In [501]: pd.concat([df, df1.set_index(df1.index + '_s')]).sort_index()
Out[501]:
                  A          B          C
first     62.184209  39.414005  60.716563
first_s    0.828069   0.762570   0.717368
second    51.508214  94.354199  16.938342
second_s   0.136098   0.991668   0.547499
third     36.081861  39.440953  38.088336
third_s    0.120465   0.546807   0.346949

      

+5


source







All Articles