Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the k highest values in a data frame in pandas?

How can I retrieve the k highest values in a data frame in pandas?

For example, given the DataFrame:

               b         d         e
Utah    1.624345 -0.611756 -0.528172
Ohio   -1.072969  0.865408 -2.301539
Texas   1.744812 -0.761207  0.319039
Oregon -0.249370  1.462108 -2.060141

Generated with:

import numpy as np
import pandas as pd
np.random.seed(1)
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), 
                     index=['Utah', 'Ohio', 'Texas', 'Oregon'])
print(frame)

The 3 highest values in the data frame are:

  1. 1.744812
  2. 1.624345
  3. 1.462108
like image 547
Franck Dernoncourt Avatar asked Aug 16 '17 15:08

Franck Dernoncourt


People also ask

How do you get the highest value in pandas?

The max() method returns a Series with the maximum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the maximum value for each row.

How will you find the top 5 records of a DataFrame?

head(n) to get the first n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the start). By default n = 5, it return first 5 rows if value of n is not passed to the method.

How do you find the highest value in a data set in Python?

Use Python's min() and max() to find smallest and largest values in your data. Call min() and max() with a single iterable or with any number of regular arguments.

How do you find the most frequent value of a data frame?

DataFrames consist of rows, columns, and data. To get the number of the most frequent value in a column, we will first access a column by using df['col_name'], and then we will apply the mode() method which will return the most frequent value.


1 Answers

You can use pandas.DataFrame.stack + pandas.Series.nlargest, e.g.:

In [183]: frame.stack().nlargest(3)
Out[183]:
Texas   b    1.744812
Utah    b    1.624345
Oregon  d    1.462108
dtype: float64

or:

In [184]: frame.stack().nlargest(3).reset_index(drop=True)
Out[184]:
0    1.744812
1    1.624345
2    1.462108
dtype: float64
like image 181
MaxU - stop WAR against UA Avatar answered Nov 03 '22 01:11

MaxU - stop WAR against UA