I have this pandas dataframe:
artist track class1 class2 class3
0 Portishead Roads 0.00 1.00 0.0
1 Yo La Tengo Our Way to Fall 0.14 0.86 0.0
2 Radiohead Fake Plastic Trees 0.03 0.97 0.0
and these two user input variables:
input_value = 0.80
input_class = 'class2'
from those variables I would like to iterate over the dataframe,
find the closest value to input_valuein chosen class2, and re-order dataframe rows, like so:
artist track class1 class2 class3
1 Yo La Tengo Our Way to Fall 0.14 0.86 0.0
2 Radiohead Fake Plastic Trees 0.03 0.97 0.0
0 Portishead Roads 0.00 1.00 0.0
where the closeness of class2 values determines the order of rows.
(0.86 being the closest to 0.80, 0.97 second to that and so on..)
so far I have only found the closest value, with the following code:
for col in df.ix[:,'class1':'class3']:
if col == input_class:
print min(df[col] - input_value)
but I'm still a bit far from my goal. can anyone point me in the right direction?