user7830303
user7830303

Reputation:

Reorder dataframe rows based on column values

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?

Upvotes: 1

Views: 1403

Answers (1)

cs95
cs95

Reputation: 402493

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

Upvotes: 3

Related Questions