Quantcast
Channel: DataFrame sorting based on a function of multiple column values - Stack Overflow
Browsing latest articles
Browse All 6 View Live

Answer by cglacet for DataFrame sorting based on a function of multiple...

Another approach, similar to this one is to use argsort which returns the indexes permutation directly:f = lambda r: r.x**2 + r.y**2df.iloc[df.apply(f, axis=1).argsort()]I think using argsort better...

View Article



Answer by andrewkittredge for DataFrame sorting based on a function of...

df.loc[(df.x ** 2 + df.y ** 2).sort_values().index]after How to sort pandas dataframe by custom order on string index

View Article

Answer by Adam Warner for DataFrame sorting based on a function of multiple...

from pandas import DataFrameimport pandas as pdd = {'one':[2,3,1,4,5],'two':[5,4,3,2,1],'letter':['a','a','b','b','c']}df = pd.DataFrame(d)#f = lambda x,y: x**2 + y**2array = []for i in range(5):...

View Article

Answer by ayhan for DataFrame sorting based on a function of multiple column...

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 two2 b 1 33 b 4 21 a 3 44 c 5 10 a 2 5

View Article

Answer by Sandeep for DataFrame sorting based on a function of multiple...

Have you tried to create a new column and then sorting on that. I cannot comment on the original post, so i am just posting my solution.df['c'] = df.a**2 + df.b**2df = df.sort_values('c')

View Article


DataFrame sorting based on a function of multiple column values

Based on python, sort descending dataframe with pandas:Given:from pandas import DataFrameimport pandas as pdd = {'x':[2,3,1,4,5],'y':[5,4,3,2,1],'letter':['a','a','b','b','c']}df = DataFrame(d)df then...

View Article
Browsing latest articles
Browse All 6 View Live


Latest Images