Libraries within Kent County Council - Data Analysis

· 4 min read
Libraries within Kent County Council - Data Analysis
Photo by Shahadat Rahman / Unsplash

Importing Tools:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.patches as mpatches
from matplotlib.pyplot import figure

Reading in the data:

data = pd.read_csv('data\KCC_Libraries.csv')

Taking a quick look at the dataset:

data.head(5)
districtlocation0-1112-1920-5960+othertotal_borrowersnumber_of_borrowed_itemscomputer_hoursnumber_of_visitsitems_in_stockstaffing_costspremises_costsother_coststotal_costsnumber_of_birth_and_death_registrations
0Tunbridge WellsCranbrook15815167285596849866167277616850170643131716851652323
1ThanetMargate3669213934307250237625503547735582670853547713814424
2CanterburyWhitstable11281392595156942488313372592726095407612592792783290
3ShepwayHythe137141595381409881410848531722362723818477132362895159399
4DoverDover118171723386370873668592765519555215614964451956253756594
data.groupby(['district']).sum().sort_values('items_in_stock', ascending=False)

0-11
12-1920-5960+othertotal_borrowersnumber_of_borrowed_itemscomputer_hoursnumber_of_visitsitems_in_stockstaffing_costspremises_costsother_coststotal_costsnumber_of_birth_and_death_registrations
district
Maidstone635132723874271263530654171785122082512091163945032082538118724166
Thanet362606431237308261029631246294161523521531054229801523547284393385
Sevenoaks7731447959462222880365241506457651387211397082513571387235297882557
Tonbridge & Malling649117709833231253932673152984061347601356561871961347624576142062
Canterbury5979496015822803513396162301133251325851337043319791325875982703716
Dover280664911222223228227249196661941311491317172923651311515552331680
Swale447746119692762377331722155108351242581249941606751242604099291788
Tunbridge Wells44891526698214197725440162163711191551197922675441191575064932994
Shepway34555446943266205524546205873241177711178422270931177734627082188
Gravesham263109423565160152015697239373861173541178451661561173564013572161
Dartford314109339313117119212724186358371121251125281702471121273949022538
Ashford34596451781150182318957183546849596596510246297959674387742810

Is the biggest library also the busiest?

big_libraries = data.sort_values("total_borrowers", ascending=False)[
    ["location", "total_borrowers", "number_of_visits"]
].head(20)
plt.figure(figsize=(8, 6), dpi=100)
plt.barh(
    big_libraries["location"],
    big_libraries["number_of_visits"],
    color="#F4B860",
    label="Total Visits",
)
plt.barh(
    big_libraries["location"],
    big_libraries["total_borrowers"],
    color="#C83E4D",
    label="Total Members",
)
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.legend()
plt.ylabel("Location")
plt.suptitle("Is the library with the biggest membership the busiest?")
plt.figtext(
    0.9,
    -0.05,
    "Data Source: https://www.kent.gov.uk/",
    horizontalalignment="right",
    fontsize=6,
)
plt.savefig("Biggest_Busiest.jpg", bbox_inches="tight")
plt.show()
total_items = (
    data.groupby("district")
    .sum()
    .sort_values("items_in_stock", ascending=False)
    .reset_index()[["district", "items_in_stock", "number_of_visits"]]
)
total_items
plt.figure(dpi=100)
plt.barh(
    total_items.district,
    total_items.items_in_stock,
    color="#F4B860",
    label="Total Library Items",
)
plt.barh(
    total_items.district,
    total_items.number_of_visits,
    color="#C83E4D",
    label="Total Visits",
)
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.legend()
plt.ylabel("Number of Items Available")
plt.suptitle("Number of items vs. Number of borrows")
plt.title("per District")
plt.figtext(
    0.9,
    -0.1,
    "Data Source: https://www.kent.gov.uk/",
    horizontalalignment="right",
    fontsize=6,
)
plt.xticks(rotation=60)
plt.savefig("Items_Borrows.jpg", bbox_inches="tight")
plt.show()
computers = data.sort_values("computer_hours", ascending=False).head(10)[
    ["location", "computer_hours"]
]
plt.figure(dpi=100)
plt.barh(computers["location"], computers["computer_hours"], color="#C83E4D")
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.legend()
plt.ylabel("Location")
plt.xlabel("Total Hours")
plt.suptitle("Number Of Computer Hours Logged Per Disctrict")
plt.figtext(
    0.9,
    -0.05,
    "Data Source: https://www.kent.gov.uk/",
    horizontalalignment="right",
    fontsize=6,
)
plt.savefig("Computer_Hours.jpg", bbox_inches="tight")
plt.show()
ages_by_district = (
    data.groupby("district")
    .sum()
    .reset_index()
    .sort_values("60 ", ascending=True)[
        ["district", "0-11", "12-19", "20-59", "60 ", "other", "total_borrowers"]
    ]
)
plt.figure(dpi=100)
plt.barh(
    ages_by_district["district"], ages_by_district["0-11"], color="#6699CC", label="0-9"
)
plt.barh(
    ages_by_district["district"],
    ages_by_district["12-19"],
    color="#4A5859",
    label="12-19",
)
plt.barh(
    ages_by_district["district"],
    ages_by_district["20-59"],
    color="#A0EEC0",
    label="20-59",
)
plt.barh(
    ages_by_district["district"], ages_by_district["60 "], color="#F4B860", label="60 "
)
plt.barh(
    ages_by_district["district"],
    ages_by_district["other"],
    color="#C83E4D",
    label="other",
)
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.legend()
plt.ylabel("Number Of Borrowers")
plt.suptitle("Number Of Borrowers Per District")
plt.figtext(
    0.9,
    -0.05,
    "Data Source: https://www.kent.gov.uk/",
    horizontalalignment="right",
    fontsize=6,
)
plt.savefig("District_Ages.png", bbox_inches="tight")
plt.show()