Plot time series data - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Plot time series data (/thread-39538.html) |
Plot time series data - schniefen - Mar-04-2023 I have a long list of data looking like this: [attachment=2269] Minute and Second are always zero since the data is the 1 hour average of a measurement, which is the last column. I would like to plot this data. If there was only two columns, date and measurement, then I would simply do: import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('data.csv') plt.plot(df)So I am considering to rewrite the dataframe, however, I am unsure if this approach will work. Appreciate any feedback. RE: Plot time series data - noisefloor - Mar-04-2023 Hi, if you combine the first six columns to one column holding a datetime object, plotting should be easy. Regards, noisefloor RE: Plot time series data - schniefen - Mar-04-2023 (Mar-04-2023, 03:56 PM)noisefloor Wrote: Hi, Here is my current solution, which I'd like to shorten if possible: import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('data.csv') df['Date']=pd.to_datetime(df[['Year', 'Month', 'Day', 'Hour', 'Minute','Second']]) df=df.drop(['Year', 'Month', 'Day', 'Hour', 'Minute','Second'], axis=1) df=df.dropna() plt.plot(df['Date'],df['HYY_META.VOC_M137_1250']) RE: Plot time series data - noisefloor - Mar-04-2023 Hi, what to you mean by "shorten"? The code is already pretty short... If you like to get rid of one line, you can skip line 5 in your code and skip deleting the year, month, ... columns, as there not a real need for it. You could also shorten the code if the input csv would have a column for datetime already instead of six columns. In case you have an impact on how the csv file is structured. Regards, noisefloor |