python - Difference between maximum and second maximum from a DataFrame -
i have dataframe wanted difference between maximum , second maximum dataframe new column appended dataframe output.
the data frame looks example (this quite huge dataframe):
gene_id time_1 time_2 time_3 0.01489251 8.00246 8.164309 b 6.67943235 0.8832114 1.048761
so far tried following it's taking headers,
largest = max(df) second_largest = max(item item in df if item < largest)
and returning header value alone.
you can define func takes values, sorts them, slices top 2 values ([:2]
) calculates difference , returns second value (as first value nan
). apply
, pass arg axis=1
apply row-wise:
in [195]: def func(x): return -x.sort(inplace=false, ascending=false)[:2].diff()[1] df['diff'] = df.loc[:,'time_1':].apply(func, axis=1) df out[195]: gene_id time_1 time_2 time_3 diff 0 0.014893 8.002460 8.164309 0.161849 1 b 6.679432 0.883211 1.048761 5.630671
Comments
Post a Comment