Python - Sorting columns and selecting top n rows in each group pandas dataframe

Given a pandas dataframe, we have to sort columns and selecting top n rows in each group.
Submitted by Pranit Sharma, on September 02, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Given a pandas dataframe, we have to sort columns and selecting top n rows in each group.

Sorting columns and selecting top n rows in each group pandas dataframe

To sort pandas DataFrame columns and then select the top n rows in each group, we will first sort the columns. Sorting refers to rearranging a series or a sequence in a particular fashion (ascending, descending, or in any specific pattern. Sorting in pandas DataFrame is required for effective analysis of the data. Sorting is done with the help of DataFrame.sort_values() method.

After sorting, we will be able to select n top rows with the help of DataFrame.head() method inside which we will pass the value to n which is the number of rows we want to select.

Let us understand with the help of an example,

Python program to sort columns and selecting top n rows in each group pandas dataframe

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d1 = {
    'Subject':['phy','che','mat','eng','com','hin','pe'],
    'Marks':[78,82,73,84,75,60,96],
    'Max_marks':[100,100,100,100,100,100,100]
}

# Creating DataFrames
df = pd.DataFrame(d1)

# Display the DataFrames
print("Original DataFrame 1:\n",df,"\n\n")

# Sorting and selecting dataframe
df = df.sort_values('Marks',ascending = True).head(3)

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Sorting columns and selecting top n rows in each group

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.