Kenya’s April fuel shortage — A look into the data

Edwin Mbugua
8 min readApr 25, 2022

--

Kenya experienced fuel shortage that got into a crisis with motorists getting stuck in long queues to fuel as major petrol stations reported lack of fuel.

Petrol Station Fuel Pumps

The nationwide fuel shortage was caused by supply hitches were expected to ease up from Thursday(April 14th 2022) after oil marketers steeped supplies to stations from depots.

Articles related to the fuel shortage and it’s cause. https://nation.africa/kenya/business/why-you-still-can-t-get-fuel-despite-uhuru-sh34bn-intervention-3773538 https://www.theeastafrican.co.ke/tea/business/fuel-crisis-kenya-to-fine-dealers-3771688

Problem Definition

The objective here is to analyze the fuel data from http://www.ikowhere.co.ke/fuel/ a recent developed website to find out how the distribution of fuel was across petrol stations in the reported counties, towns and areas from the dates of 8th- 19th April 2022.

Data

Original source of the data came from http://www.ikowhere.co.ke/fuel/ the data was scrapped to be able to use in analyzing it and was saved in a csv format. Programming Language used was Python and platform was Jupyter notebook.

Features

The Data collected and analyzed comprised of several columns denoted as :
County ,Town, Area, Road, Fuel Type, Station and Date listed

#Loading libraries to be used
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
color = sns.color_palette()
%matplotlib inline
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.tools as tls
import plotly.express as px

Web scrapping

By definition web scrapping is an automated method used to extract large amounts of data from websites. The data initially is in unstructured form through web scrapping helps in collecting it and storing it in unstructured form. Just to give an outline of the steps undertaken when scrapping

  1. Find the URL that you want to scrape
  2. Inspecting the Page
  3. Find the data you want to extract
  4. Write the code
  5. Run the code and extract the data
  6. Store the data in the required format

To perform this task we shall use Beautiful Soup which is a Python package for parsing HTML and XML documents. It is helpful to extract the data easily. Next we clean out data then use it for our analysis.

#Create an URL oject
URL= "http://www.ikowhere.co.ke/fuel/"
#Create an object page
page=requests.get(URL)
page #Response 200 means the server allows us to collect data from their website
#Parser-lxml = Change html to Python friendly format
#Obtain page's information
soup = BeautifulSoup(page.text,'lxml')
soup
"""After we have found the location of the table now we can define the variable. In this section, we define the table as ‘table1"""
#Obtain information from tag <table>
table1=soup.find('table',
id='customers')
table1

After the table1 has been created now the next thing we can do is inspecting the location of each column. If we look into each column we notice that they have the same characteristic.

After we found the tag of each column the next thing we can do is create a for loop to fill an empty list with each column. We define an empty list as headers.

#Obtain every title of columns with tag <th>
headers=[]
for i in table1.find_all('th'):
title=i.text
headers.append(title)
"""After the list of columns is made the next thing we can do is create a dataframe. We define the dataframe as mydata."""
#Create a dataframe
mydata=pd.DataFrame(columns=headers)

Fill the DataFrame

After the data frame is created now we can fill it with items in each column. Before we create a for loop, we need to identify the location of the row and item column first.

we can identify that the row is located under tag <tr>and items are located under tag <td>.This is applied to all rows and items within the table.

After founding the tags now we can create a for loop.

#Create a for loop to fill mydata
for j in table1.find_all('tr')[1:]:
row_data=j.find_all('td')
row=[i.text for i in row_data]
length=len(mydata)
mydata.loc[length]=row
#Data frame filled with data
mydata.head()
#saving our data frame in a csv file
petrol_data=mydata.to_csv('D:/DS/petrol_location_ke.csv',index=False)
#Loading the data
petrol_data=pd.read_csv('D:/DS/petrol_location_ke.csv')
#Removing the Click link for details column we shall not use it
petrol_data.drop('Click link for details',axis=1,inplace=True)

Now having cleaned our data we can now proceed with visualizing it.

Summary of the first 5 rows from our data
petrol_station=petrol_data['Station '].value_counts()
colors=['mediumturquoise','darkorange','gold','lightgreen']
trace=go.Pie(values=(np.array((petrol_station/petrol_station.sum())*100)),labels=(np.array(petrol_station.index)))
layout=go.Layout(title='Petrol Stations')
data=[trace]
fig=go.Figure(trace,layout)
fig.update_traces(marker=dict(colors=colors,line=dict(color='#000000',width=2)))
fig.show()
A general distribution of the petrol stations reported

From the pie chart above we find out that from the reported petrol stations Total petrol station was the most reported to have fuel at 33.3% followed by Shell at 29.1% ,Rubis at 21% ,Ola/Oil Libya at 3.37% and Petrocity at 2.57%.

Petrol stations such as Delta,National Oil,Luqman,Hass and Others not captured from the list were reported to have fuel but but were not the majority as compared to shell or Total as they fell below 2% and above 1%.

Visualizing fuel type distribution

fuel_type=petrol_data['Fuel Type'].value_counts()
fuel_labels=(np.array(fuel_type.index))
plt.pie(fuel_type,labels=fuel_labels,radius = 2.3,autopct='%.0f%%')
plt.legend(title='Fuel Type')
plt.show()
Fuel Type Distribution

When in comes to the most common type of fuel available, majority of the fuel stations had both Super&Diesel reported at 62% while those with only Super/Petrol without Diesel were at 31% . Diesel and V-power only fuel types were at 5% and 2% respectively available from the listed petrol stations.

Petrol Station Distribution with relation to the Fuel Type

#Petrol  Station Vs Fuel Type 
fuel_station=petrol_data.loc[:,["Fuel Type","Station "]].groupby(["Fuel Type","Station "]).size().reset_index()
fuel_station.columns=["Fuel Type","Station","Count"]
fig=px.bar(fuel_station,x='Station',y='Count',color="Fuel Type",
barmode='group',title="Petrol Station Distribution in relation to Fuel Type",
height=500,width=800)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.6)
fig.show()
Fuel Type Availability in relation to Petrol Station

Petrol stations,Total,Shell and Rubis had higher chances to find Both Super & Diesel,Super/Petrol and Diesel only fuel types compared to other petrol stations.

Here we find out that a majority of Total petrol stations had both Super and Diesel fuel type with some reported only to have Super/Petrol and Diesel fuel types. Meaning the was a higher likely wood to find both Super&Diesel fuel type at a majority of Total petrol station followed by shell and Rubis petrol stations respectively.

Shell petrol station due to the ongoing fuel shortage they had motorists buying their V-power in areas where there was no alternative.

Petrol stations Delta,Ola/Oil Libya and Petrocity had equal distribution of both Super & Diesel fuel type.

Petrol Station Distribution in relation to the Counties

#Fuel Type vs County
fuel_station=petrol_data.loc[:,["Station ","County"]].groupby(["Station ","County"]).size().reset_index()
fuel_station.columns=["Station ","County","Count"]
fig=px.bar(fuel_station,x='County',y='Count',color="Station ",
barmode='group',title="Petrol Station Distribution in relation to County",
height=500,width=1500)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.8)
fig.show()
Petrol Station Distribution in relation to County

Majority of the petrol stations reported were within Nairobi county followed by Kiambu ,Machakos,Mombasa and Nakuru counties.

Petrol Station Distribution in relation to the Area

#Fuel Type vs Area
fuel_station=petrol_data.loc[:,["Fuel Type","Area"]].groupby(["Fuel Type","Area"]).size().reset_index()
fuel_station.columns=["Fuel Type","Area","Count"]
fig=px.bar(fuel_station,x='Area',y='Count',color="Fuel Type",
barmode='group',title="Petrol Station Distribution in relation to it's Area",
height=500,width=1800)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.8)
fig.show()
Fuel Type in relation to the Area of availability

Karen are reported high availability of both Super&Diesel fuel type,followed by Nairobi’s CBD area,Westlands area ,Hurlingham,Langata,Ruiru and Ruaka areas respectively.

Petrol stations with only super/petrol were found in areas such as Gikomba,Joska,Kenyatta University,Kongowea,Mihango,Nanyuki,Racecourse,Zimmerman,Githunguri,Kahawest,Kangemi,Kwa Ng’ethe,Machakos,Nyayo stadium and Parklands.

Fuel Type Distribution with relation to Town

fuel_Area=petrol_data.loc[:,["Fuel Type","Town"]].groupby(["Fuel Type","Town"]).size().reset_index()
fuel_Area.columns=["Fuel Type","Town","Count"]
fig=px.bar(fuel_Area,x='Town',y='Count',color="Fuel Type",
barmode='group',title="Fuel Type Distribution with Town",
height=600,width=900,)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.8)
fig.show()
Reported fuel type availability with respect to Town

This is where the bias in data comes in ,from the data collection point majority of the data from Nairobi and it’s environs were easy to capture compared to outside Nairobi in Towns such as Eldoret,Nanyuki,Kilifi some road names were not indicated.

This was an abrupt solution I believe the ikowhere.co.ke team as they continue to develop their platform more data will be easily captured.

Both Super & Diesel fuel type were the most reported available fuel type from the petrol stations captured, worth noting is V-power was reported majorly available in Nairobi and it’s satellite towns, in Nanyuki Super/Petrol was the only reported fuel type.

Petrol Station fuel availability in with reported Road.

#Petrol Station vs Road
fuel_station=petrol_data.loc[:,["Station ","Road"]].groupby(["Station ","Road"]).size().reset_index()
fuel_station.columns=["Station","Road","Count"]
fig=px.bar(fuel_station,x='Road',y='Count',color="Station",
barmode='group',title="Petrol Station fuel availability vs Reported Road",
height=500,width=4000)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.8)
fig.show()
Fuel Type Availability with respect to the Road
Fuel Type Availability with respect to the Road

The Petrol stations along Mombasa rd were the majority reported to have most of the fuel types, followed by those along Thika rd , Waiyaki way,Langata rd ,Ngong rd Limuru rd ,Kangundo rd,Argwings Kodhek,Eastern bypass and the Nairobi-Nakuru rd A104 were the top 10 petrol stations most reported.

Petrol Station Distribution based on Date

fuel_date=petrol_data.loc[:,["Station ","Date"]].groupby(["Station ","Date"]).size().reset_index()
fuel_date.columns=["Station ","Date","Count"]
fig=px.bar(fuel_date,x="Date",y='Count',color="Station ",
barmode='group',title="Date Distribution and Station",
height=600,width=1100)
fig.update_traces(marker_line_color='rgb(9,50,100)',marker_line_width=1.5,opacity=0.8)
fig.show()
Availability of fuel type with respect to the reported Date.

From the uploaded data most reporting happened between Date 13 and 15 of April 2022.This is the same week that guys got to learn about the site and also when the revised petroleum prices were to begin from the 15th.

Conclusion

Worth noting is after 15th with the new prices fuel was available across the petrol station hence the low uptake to report availability of fuel.

Most motorists fueled at Total or Shell followed by Rubis.

The website got a spike of guys uploading information during the peek of the shortage from the dates of 13–15th April 2022.

The focus is mostly covering Nairobi and it’s environs it would be also great to have other towns details captured well as mostly are left as None or Other (specific rd name)

This analysis was based on a small dataset just to highlight how the fuel availability fared across the petrol stations in the areas reported future work would involve capturing a large dataset. Kudos team ikowhere for this.
A machine learning model could be be explored using this dataset to predict the availability of fuel in a give petrol station given a set of features.

Looking forward to your feedback help a brother be better to make this better.

--

--

Edwin Mbugua

Loves to build things that solves day to day African challenges using technology currently biasharabook.com formerly moviemtaani.co.ke