# Demand and Supply Analysis using Python

Demand and Supply Analysis is a valuable concept used to analyze the relationship between the quantity demanded and the quantity supplied. If you want to learn about demand and supply analysis and how it helps businesses in the real world, this article is for you. In this article, I will take you through the task of Demand and Supply Analysis using Python.

## Demand and Supply Analysis

The demand for a product or service is the quantity of that product or service the customers are willing to purchase at any given price during a particular period. And the supply for a product or service is the quantity the producers are willing to provide in the market at a particular price and a particular period.

Demand and Supply analysis means analyzing the relationship between the quantity demanded and the quantity supplied. It helps businesses understand the factors influencing consumer demand to maximize profits.

For the task of Demand and Supply analysis, we need a dataset based on demand for a product or service and supply for a product or service. I found an ideal dataset for this task, which is based on the demand for cab rides at a given time and the availability of drivers at a given time. You can download the dataset from here.

## Demand and Supply Analysis using Python

I will start the task of demand and supply analysis by importing the necessary Python libraries and the dataset:

```import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.templates.default = "plotly_white"

```   Drivers Active Per Hour  Riders Active Per Hour  Rides Completed
0                       72                     295            202.0
1                       50                      78             43.0
2                       40                     250            181.0
3                       78                     140            124.0
4                       74                     195            108.0```

Before moving forward, let’s have a look if the dataset has null values or not:

`print(data.isnull().sum())`
```Drivers Active Per Hour     0
Riders Active Per Hour      0
Rides Completed            54
dtype: int64```

The dataset has 54 null values in the Rides Completed column. I’ll drop these rows and move forward:

`data = data.dropna()`

Now let’s analyze the relationship between the number of drivers active per hour and the number of riders active per hour:

```demand = data["Riders Active Per Hour"]
supply = data["Drivers Active Per Hour"]

figure = px.scatter(data, x = "Drivers Active Per Hour",
y = "Riders Active Per Hour", trendline="ols",
title="Demand and Supply Analysis")
figure.update_layout(
xaxis_title="Number of Drivers Active per Hour (Supply)",
yaxis_title="Number of Riders Active per Hour (Demand)",
)
figure.show()```

So there is a constant relationship between the number of drivers active per hour and the number of riders active per hour.

A constant relationship between the number of drivers active per hour and the number of riders active per hour means that for every X number of drivers, there is a consistent and predictable Y number of riders, and this ratio remains constant over time.

Now let’s calculate the elasticity of demand for rides concerning the number of active drivers per hour:

```# Calculate elasticity
avg_demand = data['Riders Active Per Hour'].mean()
avg_supply = data['Drivers Active Per Hour'].mean()
pct_change_demand = (max(data['Riders Active Per Hour']) - min(data['Riders Active Per Hour'])) / avg_demand * 100
pct_change_supply = (max(data['Drivers Active Per Hour']) - min(data['Drivers Active Per Hour'])) / avg_supply * 100
elasticity = pct_change_demand / pct_change_supply

print("Elasticity of demand with respect to the number of active drivers per hour: {:.2f}".format(elasticity))```
```Elasticity of demand with respect to the number of active drivers per hour: 0.82
```

It signifies a moderately responsive relationship between the demand for rides and the number of active drivers per hour. Specifically, this means that a 1% increase in the number of active drivers per hour would lead to a 0.82% decrease in the demand for rides, while a 1% decrease in the number of active drivers per hour would lead to a 0.82% increase in the demand for rides.

This level of elasticity suggests that the demand for rides is somewhat sensitive to changes in the number of active drivers per hour.

Now let’s add a new column in the dataset by calculating the supply ratio:

```# Calculate the supply ratio for each level of driver activity
data['Supply Ratio'] = data['Rides Completed'] / data['Drivers Active Per Hour']
```   Drivers Active Per Hour  Riders Active Per Hour  Rides Completed  \
0                       72                     295            202.0
1                       50                      78             43.0
2                       40                     250            181.0
3                       78                     140            124.0
4                       74                     195            108.0

Supply Ratio
0      2.805556
1      0.860000
2      4.525000
3      1.589744
4      1.459459  ```

Now let’s visualize the supply ratio:

```fig = go.Figure()
y=data['Supply Ratio'], mode='markers'))
fig.update_layout(
title='Supply Ratio vs. Driver Activity',
xaxis_title='Driver Activity (Drivers Active Per Hour)',
yaxis_title='Supply Ratio (Rides Completed per Driver Active per Hour)'
)
fig.show()```

The above graph shows the ratio of the number of drivers active per hour and the number of rides completed in an hour. So this is how we can analyze demand and supply using the Python programming language.

### Summary

Demand and Supply analysis means analyzing the relationship between the quantity demanded and the quantity supplied. It helps businesses understand the factors influencing consumer demand to maximize profits. I hope you liked this article on Demand and Supply Analysis using Python. Feel free to ask valuable questions in the comments section below. ##### Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498