DataFrame sorting based on a function of multiple column values

前端 未结 5 860
我在风中等你
我在风中等你 2020-12-06 05:11

Based on python, sort descending dataframe with pandas:

Given:

from pandas import DataFrame
import pandas as pd

d = {\'x\':[2,3,1,4,5],
     \'y\':[         


        
5条回答
  •  再見小時候
    2020-12-06 05:27

    Another approach, similar to this one is to use argsort which returns the indexes permutation directly:

    f = lambda r: r.x**2 + r.y**2
    df.iloc[df.apply(f, axis=1).argsort()]
    

    I think using argsort better translates the idea than a regular sort (we don't care about the value of this computation, only about the resulting indexes).

    It could also be interesting to patch the DataFrame to add this functionality:

    def apply_sort(self, *, key):
        return self.iloc[self.apply(key, axis=1).argsort()]
    
    pd.DataFrame.apply_sort = apply_sort
    

    We can then simply write:

    >>> df.apply_sort(key=f)
    
       x  y letter
    2  1  3      b
    3  4  2      b
    1  3  4      a
    4  5  1      c
    0  2  5      a
    

提交回复
热议问题