python - Pandas DataFrame merge choosing the higher value -
i have 2 dataframes 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 merge them choosing each time higher value. values float. ideas? have loop on dataframes?
you need concat
first , groupby
index
, 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
Comments
Post a Comment