Get top biggest values from each column of the pandas.DataFrame
dataframe, pandas, python
Solution
Create a function to return the top three values of a series:
def sorted(s, num):
tmp = s.sort_values(ascending=False)[:num] # earlier s.order(..)
tmp.index = range(num)
return tmp
Apply it to your data set:
In [1]: data.apply(lambda x: sorted(x, 3))
Out[1]:
first second third
0 89 76 98
1 56 45 87
2 40 45 67
Problem
Here is my `pandas.DataFrame`: ``` import pandas as pd data = pd.DataFrame({ 'first': [40, 32, 56, 12, 89], 'second': [13, 45, 76, 19, 45], 'third': [98, 56, 87, 12, 67] }, index = ['first', 'second', 'third', 'fourth', 'fifth']) ``` I want to create a new `DataFrame` that will contain top 3 values from each column of my `data` `DataFrame`. Here is an expected output: ``` first second third 0 89 76 98 1 56 45 87 2 40 45 67 ``` How can I do that?