Skip to content
pandas - read CSV, convert columns, calculate average, plot bar chart

pandas - read CSV, convert columns, calculate average, plot bar chart

I had speedtest-cli running on a regular basis to figure out which provider mobile provider works best in a given location. For this, I gathered all speedtest results in a CSV and just wanted to get the average download and upload speed over all entries.

This is how the CSV looks like:

server nameserver ididle latencyidle jitterpacket lossdownloaduploaddownload bytesupload bytesshare urldownload server countdownload latencydownload latency jitterdownload latency lowdownload latency highupload latencyupload latency jitterupload latency lowupload latency highidle latency lowidle latency high
foobar133733.12528.44775010673314368511229645640251281737.56485.595639.4241710.7323.5669.919635.317102326.97544.814

and here’s the python script how to convert all the things and display them:

import pandas as pd
import matplotlib.pyplot as plt

"""read csv"""

df = pd.read_csv('results.csv')

"""convert bytes to mbps"""

def bytes_to_mbits(num_bytes):
    return num_bytes * 8 / 1e6

"""take just the download and upload columns from the csv file, convert each entries using the function above, round to two decimal points"""

dl_ul = df[['download', 'upload']]
dl_ul.reset_index(drop=True)

download = round(df['download'].apply(bytes_to_mbits), 2)
upload = round(df['upload'].apply(bytes_to_mbits), 2)

"""take the download and upload results, calculate average, put on bar chart"""

plt.bar(['download', 'upload'], [download.mean(), upload.mean()], color=['green', 'red'])
plt.ylabel('speed in mbit/s')
plt.title('speedtest')
plt.show()