A dataframe is a commonly used 2-dimensional data structure. It is a table with columns and rows and is used primarily as a pandas object.
It requires the pandas
library, as shown below.
import pandas as pd
A dataframe can be formed as shown below. The following is a dataframe that contains countries that have been put in different groups and are given different
import pandas as pda_score = [4, 5, 7, 8, 2, 3, 1, 6, 9, 10]b_score = [1, 2, 3, 4, 5, 6, 7, 10, 8, 9]country = ['Pakistan', 'USA', 'Canada', 'Brazil', 'India', 'Beligium', 'Malaysia', 'Peru', 'England', 'Scotland']groups = ['A','A','B','A','B','B','C','A','C','C']df = pd.DataFrame({'group':groups, 'country':country, 'a_score':a_score, 'b_score':b_score})print(df)
The rank
function assigns ranks to values. For [13,5,11,9], the assigned ranks would be [4,1,3,2]. 5 is the smallest number, so it gets ranked 1, followed by 9, which is bigger than 5, so it gets 2, and so on.
The function prototype is as follows.
df['ranked'] = df['a_score'].rank()
The values you want to rank are the parameters.
The respective ranks are returned.
The following example assigns ranks to a_score values.
import pandas as pda_score = [4, 5, 7, 18, 12, 13, 1, 16, 9, 10]b_score = [1, 2, 3, 4, 5, 6, 7, 10, 8, 9]country = ['Pakistan', 'USA', 'Canada', 'Brazil', 'India', 'Beligium', 'Malaysia', 'Peru', 'England', 'Scotland']groups = ['A','A','B','A','B','B','C','A','C','C']df = pd.DataFrame({'group':groups, 'country':country, 'a_score':a_score, 'b_score':b_score})df['ranked'] = df['a_score'].rank()print(df)