Quantcast
Viewing all articles
Browse latest Browse all 6

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

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):    array.append(df.ix[i,1]**2 + df.ix[i,2]**2)array = pd.DataFrame(array, columns = ['Sum of Squares'])test = pd.concat([df,array],axis = 1, join = 'inner')test = test.sort_index(by = "Sum of Squares", ascending = True).drop('Sum of Squares',axis =1)

Just realized that you wanted this:

    letter  one  two2      b    1    33      b    4    21      a    3    44      c    5    10      a    2    5

Viewing all articles
Browse latest Browse all 6

Trending Articles