1

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?

1 Answer 1

3

Try argsort on the difference + iloc:

df = df.iloc[(df[input_class] - input_value).argsort()]

df
        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

Alternatively, you could use np.argsort to the same effect.

df = df.iloc[np.argsort(df[input_class] - input_value)]

df
        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

Use reset_index to reorder the index.

df.result.reset_index(drop=1)     
        artist               track  class1  class2  class3
0  Yo La Tengo     Our Way to Fall    0.14    0.86     0.0
1    Radiohead  Fake Plastic Trees    0.03    0.97     0.0
2   Portishead               Roads    0.00    1.00     0.0
Sign up to request clarification or add additional context in comments.

4 Comments

Maybe I've been up too long... but I didn't get what was going on (-:
@piRSquared yes... I surprised myself with this too ;-)
I came to your answer firstly ... and thought it should be a hard question ..LOL
it is do the same trick like order in R :-), thanks, learn new thing ~