Doing More With CSV Data (Python)

Opening CSV data with Pandas

Yesterday I outputted sensor readings into a CSV file, today I’m going to look at what I can do with that CSV file in Python.

Pandas

I originally looked at Pandas back in December 2018 when I used it to open an Excel spreadsheet. A CSV is open in a similar way:

import pandas

rf = pandas.read_csv(‘sensor_readings.csv’, index_col=’date’)
print(rf)

These few lines open the CSV, uses the “date” column as the index and then outputs the data in the CSV.

Opening CSV data with Pandas
Opening CSV data with Pandas

MatPlotLib

MatPlotLib is a Python library that helps to create charts, which is a good way to show the readings from the Raspberry Pi sensor that the csv contains.

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open(‘sensor_readings_2.csv’,’r’) as csvfile:
plots = csv.reader(csvfile, delimiter=’,’)
for row in plots:
        x.append(float(row[1]))
        y.append(float(row[2]))
plt.plot(x,y,linewidth=5)
plt.title(“GeekTechStuff Temperature”)
plt.xlabel(“Time”)
plt.ylabel(“Temperature”)
plt.show()

Matplotlib plotting a chart
Matplotlib plotting a chart

I adjusted my sensor output a little for the chart by dropping the seconds and changing the H:M separator from : to a . so that it became H.M this allowed Matplotlib to easily keep the time as a floating point number rather than a string (which seemed to be causing some issues with the plotting). To replace : with . I used the replace function of the Pi’s text editor. I copied the data for the 10th March 2019 (2019-03-10) into a new file called sensor_readings_2.csv and read that for this program.

The same process can be used to create a scatter graph by changing the line:

plt.plot(x, y, linewidth=5)

to:

plt.scatter(x,y)

Matplotlib scatter graph
Matplotlib scatter graph

 

 

 

One thought on “Doing More With CSV Data (Python)

Comments are closed.