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.
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:
rf = pandas.read_csv(‘sensor_readings.csv’, index_col=’date’)
These few lines open the CSV, uses the “date” column as the index and then outputs the data in the CSV.
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
x = 
y = 
with open(‘sensor_readings_2.csv’,’r’) as csvfile:
plots = csv.reader(csvfile, delimiter=’,’)
for row in plots:
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)
One thought on “Doing More With CSV Data (Python)”
Comments are closed.