Analyzing T20 Cricket Records: Babar Azam vs. Virat Kohli with Pandas and Matplotlib

Haris
Stackademic
Published in
2 min readMay 9, 2024

--

Photo by michael weir on Unsplash

Cricket enthusiasts around the world often engage in debates over the best players in the game. In the fast-paced world of T20 cricket, the performances of top players like Babar Azam and Virat Kohli are closely scrutinized. In this article, we’ll delve into using Python’s Pandas and Matplotlib libraries to analyze and visualize the T20 records of these two cricketing stalwarts.

Importing the Libraries and Data:

import pandas as pd
import matplotlib.pyplot as plt

# Define the path to the CSV file containing T20 statistics
path_to_file = "C:/Users/<define_your_own_path/Downloads/t20_stats.csv - Sheet1.csv"

# Read the CSV file into a Pandas DataFrame
df = pd.read_csv(path_to_file)

# Set the first column as the index (assuming it contains the names of the statistics)
df.set_index(df.columns[0], inplace=True)

# Extract statistics for Virat Kohli and Babar Azam
virat_stats = df['Virat Kohli']
babar_stats = df['Babar Azam']

Data Preparation:

# Create a DataFrame to store the comparison
comparison_df = pd.DataFrame({'Virat Kohli': virat_stats, 'Babar Azam': babar_stats})

# Determine when Babar Azam outperforms Virat Kohli and vice versa
babar_better_rows = babar_stats > virat_stats
virat_better_rows = virat_stats > babar_stats

Data Visualization:

# Plotting the comparison
plt.figure(figsize=(15, 9))
comparison_df.plot(kind='bar')

# Adding titles and labels
plt.title('Virat Kohli vs. Babar Azam - T20 Cricket Records')
plt.xlabel('Statistics')
plt.ylabel('Values')
plt.xticks(rotation=45)
plt.tight_layout()

# Highlighting instances where Babar Azam outperforms Virat Kohli
for i, babar_better_row in enumerate(babar_better_rows):
if babar_better_row:
plt.axvline(x=i, color='green', linestyle='--')

# Highlighting instances where Virat Kohli outperforms Babar Azam
for i, virat_better_row in enumerate(virat_better_rows):
if virat_better_row:
plt.axvline(x=i, color='blue', linestyle='--')

plt.show()
Data Visualization

Note that the data for the .csv file comes from this source.

Analyzing T20 cricket records using Pandas and Matplotlib provides valuable insights into the performances of players like Babar Azam and Virat Kohli. By visualizing their statistics side by side, cricket enthusiasts can gain a better understanding of their strengths and weaknesses on the T20 stage. This approach not only facilitates data-driven comparisons but also enhances the appreciation of these cricketing icons’ remarkable skills.

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--

Senior Software Engineer / Salesforce Technology / Python Web Development / Data Analytics / Scripting