How to use the pct_change function on a dataframe in pandas

What is a DataFrame?

A DataFrame is a commonly used 2-dimensional data structure.

It is a table that consists of columns and rows and is used primarily as an object in pandas.

Reqiurements

This requires the pandas library, as shown below:


import pandas as pd

Code example

A DataFrame can be formed as shown below. This contains countries that have been put in different groups and are given a different a_score and b_score.

Both the scores are imaginary values for this example:

import pandas as pd
a_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 pct_change() function

The pct_change function calculates the percentage change in the values through a series. For example, if the values are [2,3,6], the returned values would be [NaN, 0.5, 1.0]. This is because there is a 50% increase from the first element to the second and a 100% increase from the second element to the third.

Syntax

The function prototype is as follows:


mychange = df.a_score.pct_change()

Parameter

The function takes no parameter.

Return value

The return value of this method is the percentage change values of the elements in the series.

Example

The following example prints the percentage change values in a_score:

import pandas as pd
a_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}) #creating dataframe
print("the main dataframe")
print(df) #printing initial dataframe
print("")
print("change ")
print(df.a_score.pct_change()) #applying pct_change based on a_score

Free Resources