0

I have two dataFrames. Both have datetime as index.

                     PM 2.5  PM 10  entry_id
created_at
2018-06-13 16:11:43    4.67   5.17        20
2018-06-13 16:12:43    5.04   5.59        21
2018-06-13 16:13:43    5.40   6.10        22
2018-06-13 16:14:43    5.03   5.55        23
2018-06-13 16:15:43    4.93   5.44        24

                          action              end_at
done_at
2018-11-10 10:15:00  Babymassage 2018-11-10 12:00:00
2018-11-24 10:15:00  Babymassage 2018-11-24 12:00:00
2018-12-01 10:15:00  Babymassage 2018-12-01 12:00:00
2018-12-01 14:30:00  staubsaugen                 NaT
2018-12-01 15:30:00  Ausser Haus 2018-12-03 12:00:00

I want to plot the time on x and the PM 2.5 and PM 10 on y. And the action column from the second dataframe should be a text label with x from index. Is that possible with pandas? I try this but doesent work

#Import Modules
from sql_connection import sql_connector
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from datetime import date

#Get data from SQL server
df = pd.read_sql("feinstaub_wohnzimmer",con=sql_connector("mysql","localhost","3306","root","xxx","feinstaub"),index_col="created_at")
df1 = pd.read_sql("wohnzimmer_aktionen",con=sql_connector("mysql","localhost","3306","root","xxx","feinstaub"),index_col="done_at")

#Sort index
df = df.sort_index(axis=0)
df1 = df1.sort_index(axis=0)

#Group by Day 
dfs = dict(tuple(df.groupby(df.index.strftime('%Y-%m-%d'))))
dfd = dict(tuple(df1.groupby(df1.index.strftime('%Y-%m-%d'))))

#Plot for Today
today = str(date.today())
ax = dfd[today].plot(marker="o", label="action")
dfs[today].plot( y=["PM 2.5","PM 10"], kind='line', ax=ax)
plt.show()

It should be look like this enter image description here or do you think it is possible to plot also a sign for the duration of the action when i use the additional iformation at the end_at column?

2
  • Is there a reason you're putting the data frames in tuples nested in dictionaries?
    – Puhtooie
    Dec 8, 2018 at 2:31
  • Just spent the last hour trying to plot this, I have pretty much no idea what you're trying to do. I kept wanting to quit, but felt like what you were asking was so simple that I must have been missing something. The numerical data you provided doesn't even plot like the graph you're looking to get. strftime throws up errors when I try to run it, and even when I take that out there's bugs on bugs on bugs. 10/10 would not try to code this again.
    – Puhtooie
    Dec 8, 2018 at 3:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.