Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas sort lambda function

Tags:

Given a dataframe a with 3 columns, A , B , C and 3 rows of numerical values. How does one sort all the rows with a comp operator using only the product of A[i]*B[i]. It seems that the pandas sort only takes columns and then a sort method.
I would like to use a comparison function like below.

f = lambda i,j: a['A'][i]*a['B'][i] < a['A'][j]*a['B'][j]
like image 347
Tom B Avatar asked Sep 16 '16 07:09

Tom B


People also ask

How do you sort using lambda function?

The list. sort() method key parameter is set to lambda. The arguement x is the iterable element ( tuple ) to be sorted by the second element, the number. The lambda expression sorts the list by the second element of the tuple value and updates the original.

How do you sort a lambda function in Python?

Use the list sort() method to sort a list with a lambda expression in Python. Just call the list. sort(key=None) with a key set to a lambda expression to sort the elements of the list by the key. Both list.

What is sort key lambda Python?

To define lambda, you specify the object property you want to sort and python's built-in sorted function will automatically take care of it. If you want to sort by multiple properties then assign key = lambda x: (property1, property2). To specify order-by, pass reverse= true as the third argument(Optional.

How do I sort values in pandas series?

Sort the Series in Ascending Order By default, the pandas series sort_values() function sorts the series in ascending order. You can also use ascending=True param to explicitly specify to sort in ascending order. Also, if you have any NaN values in the Series, it sort by placing all NaN values at the end.

How to use lambda function in pandas Dataframe?

In Pandas, we have the freedom to add different functions whenever needed like lambda function, sort function, etc. We can apply a lambda function to both the columns and rows of the Pandas data frame. Example 1: Applying lambda function to single column using Dataframe.assign ()

How to sort pandas Dataframe?

How to Sort Pandas DataFrame? - GeeksforGeeks How to Sort Pandas DataFrame? In this article, we will discuss how to sort Pandas Dataframe. Let’s create a dataframe. In order to sort the data frame in pandas, function sort_values () is used.

How to sort a list with Lambda in Python?

Sort With Lambda in Python Python Python Sort Created: March-27, 2021 In Python, we have sorted() and sort() functions available to sort a list. These functions allow us to sort the list in the required order. By default, we can key ...

What is X in lambda function in Python?

First, x represents the bound variable. The second x represents the body of the function that has to be implemented. The watchword is compulsory, and it must be a lambda, while the contentions and body can change dependent on the necessities. How does Lambda Function work in Pandas?


2 Answers

There are at least two ways:

Method 1

Say you start with

In [175]: df = pd.DataFrame({'A': [1, 2], 'B': [1, -1], 'C': [1, 1]})

You can add a column which is your sort key

In [176]: df['sort_val'] = df.A * df.B

Finally sort by it and drop it

In [190]: df.sort_values('sort_val').drop('sort_val', 1)
Out[190]: 
   A  B  C
1  2 -1  1
0  1  1  1

Method 2

Use numpy.argsort and then use .ix on the resulting indices:

In [197]: import numpy as np

In [198]: df.ix[np.argsort(df.A * df.B).values]
Out[198]: 
   A  B  C
0  1  1  1
1  2 -1  1
like image 91
Ami Tavory Avatar answered Sep 23 '22 22:09

Ami Tavory


Another way, adding it here because this is the first result at Google:

df.loc[(df.A * df.B).sort_values().index]

This works well for me and is pretty straightforward. @Ami Tavory's answer gave strange results for me with a categorical index; not sure it's because of that though.

like image 25
srs Avatar answered Sep 24 '22 22:09

srs