DataFrame sorting based on a function of multiple column values

前端 未结 5 869
我在风中等你
我在风中等你 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:26

    You can create a temporary column to use in sort and then drop it:

    df.assign(f = df['one']**2 + df['two']**2).sort_values('f').drop('f', axis=1)
    Out: 
      letter  one  two
    2      b    1    3
    3      b    4    2
    1      a    3    4
    4      c    5    1
    0      a    2    5
    

提交回复
热议问题