Home > OS >  How to calculate comparison highest value to lowest value in pandas dataframe
How to calculate comparison highest value to lowest value in pandas dataframe

Time:06-16

Here's my dataset

a.age              15-20       21-34       35-50         <15         >50
829             0.973257    0.960501    0.953244    0.966997    0.957140
1030            0.943649    0.921949    0.890820    0.948845    0.902489

Here's my expected output Comparison is the highest comparing to the lowest age group

a.age              15-20       21-34       35-50         <15         >50           comparison
829             0.973257    0.960501    0.953244    0.966997    0.957140    1.020994624671123        
1030            0.943649    0.921949    0.890820    0.948845    0.902489   1.0593037875216094

Note:

a.age is index column

Comparison had 1.020994624671123 value is come from 0.973257/0.953244, 15-20 compare to 35-50

Comparison had 1.020994624671123 value is come from 0.943649/0.890820, <15 compare to 35-50

CodePudding user response:

Aggregate age like columns with min and max along axis=1, then divide max / min value to calculate ratio:

s = df.agg(['min', 'max'], axis=1)
df['comparision'] = s['max'] / s['min']

   a.age     15-20     21-34     35-50       <15       >50  comparision
0    829  0.973257  0.960501  0.953244  0.966997  0.957140     1.020995
1   1030  0.943649  0.921949  0.890820  0.948845  0.902489     1.065137

Note: I am assuming that column a.age is already set as the index of dataframe.

  • Related