Home » Python » python – How can I plot data vs time with matplotlib from a csv file?-Exceptionshub

python – How can I plot data vs time with matplotlib from a csv file?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’m sorry if this a stupid question, but I looked around at different examples and tried them before writing this.

So I have this csv file that I looks as follows

 DateTime                        Column_2                   Column_3
 9/1/2019  7:07:46 AM            894                        55
 9/1/2019  7:17:46 AM            895                        58
 9/1/2019  7:27:46 AM            898                        63

Now my code:

import pandas as pd
from matplotlib import pyplot as plt
data=pd.read_csv('data file.csv')
data=data[data.Column_2 != '(null)']
data[data.Column_3 != '(null)']
data[['Column_2','Column_3']]= [['Column_2','Column_3']].apply(pd.to_numeric, errors='coerce')
plt.plot(data.DataTime,data.Column_2)

In the above example, I removed some null values in columns 2 and 3, then converted them into int values (they were objects). The DataTime column is an object though, how can I graph the column against the time? Do I change the DataTime to an integer?

How to&Answers: