Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame merge choosing the higher value

I have two DataFrames like this:

         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 merge them choosing each time the higher value. All the values are float. Any ideas? I have to loop on the DataFrames?

like image 824
Max Avatar asked Oct 27 '25 03:10

Max


2 Answers

You need concat first and then groupby by index and aggregate 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 need 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
like image 55
jezrael Avatar answered Oct 28 '25 17:10

jezrael


df1.where(df1>df2, df2)

is doing same job, but not faster than np.where

like image 27
pakira79 Avatar answered Oct 28 '25 18:10

pakira79