w3resource

Pandas: Get lowest n records within each group of a given DataFrame

Pandas: DataFrame Exercise-81 with Solution

Write a Pandas program to get lowest n records within each group of a given DataFrame.

Sample Solution :

Python Code :

import pandas as pd
d = {'col1': [1, 2, 3, 4, 7, 11], 'col2': [4, 5, 6, 9, 5, 0], 'col3': [7, 5, 8, 12, 1,11]}
df = pd.DataFrame(data=d)
print("Original DataFrame")
print(df)
print("\nLowest n records within each group of a DataFrame:")
df1 = df.nsmallest(3, 'col1')
print(df1)
df2 = df.nsmallest(3, 'col2')
print(df2)
df3 = df.nsmallest(3, 'col3')
print(df3)

Sample Output:

Original DataFrame
   col1  col2  col3
0     1     4     7
1     2     5     5
2     3     6     8
3     4     9    12
4     7     5     1
5    11     0    11

Lowest n records within each group of a DataFrame:
   col1  col2  col3
0     1     4     7
1     2     5     5
2     3     6     8
   col1  col2  col3
5    11     0    11
0     1     4     7
1     2     5     5
   col1  col2  col3
4     7     5     1
1     2     5     5
0     1     4     7

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to check for inequality of two given DataFrames.
Next: Pandas String and Regular Expression Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.