Pandas Concatenate DataFrame, choosing higher value

I have two DataFrames:

         1          2          3 
0   61.579   0.000000  47.279861
1    0.000   0.000000   0.000000
2   62.700   9.180000  48.479861
3   56.100  40.180000  71.679861
4   73.100  50.930000  71.679861
5   88.300  37.930000  36.479861 

      

I need to combine them, choosing a larger value each time. All values ​​are floats. Any ideas? Do I need to loop over DataFrames?

+3


source to share


3 answers


You need first and then by and aggregate : concat

groupby

index

max

df1 = pd.DataFrame({0:[4,5,4],
                    1:[7,8,9]})

print (df1)
   0  1
0  4  7
1  5  8
2  4  9


df2 = pd.DataFrame({0:[8,5,6],
                    1:[9,4,4]})

print (df2)
   0  1
0  8  9
1  5  4
2  6  4

df = pd.concat([df1, df2]).groupby(level=0).max()
print (df)
   0  1
0  8  9
1  5  8
2  6  9

      



If you need a faster solution, use : numpy.where

a = df1.values
b = df2.values
df = pd.DataFrame(np.where(a > b, a, b), index=df1.index, columns=df1.columns)
print (df)
   0  1
0  8  9
1  5  8
2  6  9

      

+7


source


You can use df.where

:

# jezrael data

In [78]: df1 = pd.DataFrame({0:[4,5,4],
    ...:                     1:[7,8,9]})

In [79]: df2 = pd.DataFrame({0:[8,5,6],
    ...:                     1:[9,4,4]})

      




In [81]: df1.where(df1 > df2, df2)
Out[81]: 
   0  1
0  8  9
1  5  8
2  6  9

      

+1


source


df1.where(df1>df2, df2)

      

does the same job, but no faster than np.where

0


source







All Articles