Thursday, 9 December 2021

Write a program to demonstrate Data Frames sorting and ranking in PYTHON

 To rank the rows of Pandas DataFrame we can use the DataFrame.rank() method which returns a rank of every respective index of a series passed. The rank is returned on the basis of position after sorting.

The following program illustrates how to rank a DataFrame data

# import the required packages
import pandas as pd
# Define the dictionary for converting to dataframe
movies = {'Name': ['The Godfather', 'Bird Box', 'Fight Club'],
		'Year': ['1972', '2018', '1999'],
		'Rating': ['9.2', '6.8', '8.8']}
df = pd.DataFrame(movies)
print(" Before ranking")
print(df)
# Create a column Rating_Rank which contains
# the rank of each movie based on rating
df['Rating_Rank'] = df['Rating'].rank(ascending = 1)

# Set the index to newly created column, Rating_Rank
df = df.set_index('Rating_Rank')
print("After Ranking")
print(df)

The following is the output for above program


Sorting

There are two kinds of sorting available in Pandas. They are −

By label
By Actual Value
 

Let us consider an example with an output.

import pandas as pd
import numpy as np
unsorted_df=pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],
columns=['col2','col1'])
print(unsorted_df)

 Output:-

 


By Label 

Using the sort_index() method, by passing the axis arguments and the order of sorting, DataFrame can be sorted. By default, sorting is done on row labels in ascending order.

import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],
                columns = ['col2','col1'])
sorted_df=unsorted_df.sort_index()
print(sorted_df)


Output:-


By Value
 

Like index sorting, sort_values() is the method for sorting by values. It accepts a by argument which will use the column name of the DataFrame with which the values are to be sorted.

The following illustrates how to sort data by value

import pandas as pd
import numpy as np
unsorted_df = pd.DataFrame({'col1':[2,1,1,1],'col2':[1,3,2,4]})
sorted_df = unsorted_df.sort_values(by='col1')
print(sorted_df)

Output:-

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top