adek111
adek111

Reputation: 1458

Pandas: group dataframe by time of a day and make a plot on one figure

I've seen dozens of similar questions but I couldn't make a final effect like I want.

Data frame structure is filled with measurements taken nearly every minute (25k+ rows):

                                     some_val
...
2020-02-22 00:00:00                  1.1
2020-02-22 00:01:01                  1.4
2020-02-22 00:02:01                  1.5
...
2020-02-23 00:00:05                  1.7
2020-02-23 00:01:05                  1.6
2020-02-23 00:02:06                  1.6
...
2020-02-24 00:00:02                  1.4
2020-02-24 00:01:03                  1.8
2020-02-24 00:02:03                  1.3

I want to group that data frame by each minute of a day - so first group (time of a day 00:00) should consist of [1.1, 1.7, 1.4] and so on.

Then I want to see some trends by plotting it on 1 figure where:

For now I don't want to count() or mean() anything. Just simple grouping, plotting everything on 1 figure and saving it to file.

As I read I should be able to use pandas groupby or resample along with matplotlib.pyplot but somehow I couldn't manage to do it.

Thanks in advance.

Upvotes: 0

Views: 1042

Answers (2)

wwnde
wwnde

Reputation: 26686

If you need each minute in a day please try;

data={'Date':['2020-02-22T00:00:00.000000000', '2020-02-22T00:01:00.000000000',
       '2020-02-22T00:02:00.000000000','2020-02-23T00:00:00.000000000', '2020-02-23T00:01:00.000000000',
       '2020-02-23T00:02:00.000000000'],'some_val':[1.1, 1.4, 1.5,2.1, 2.4, 2.5]}
    df=pd.DataFrame.from_dict(data)
    df['Date']=pd.to_datetime(df['Date'])
   df.set_index(df['Date'], inplace=True)
   df['x']= df.index.minute
   for x, df2 in df.groupby(df.index.date):
    df2.plot(x='x', y='some_val')
    plt.title(x)

Upvotes: 0

wwnde
wwnde

Reputation: 26686

Solution Code

import pandas as pd

data={'Date':['2020-02-22T00:00:00.000000000', '2020-02-22T00:01:00.000000000',
           '2020-02-22T00:02:00.000000000'],'some_val':[1.1, 1.4, 1.5]}
    df=pd.DataFrame.from_dict(data)

Plotting

import matplotlib .pyplot as plt
df['Date']=pd.to_datetime(df['Date'])#Converting Date to datetime 
df.set_index(df['Date'], inplace=True)#Setting Date as index
df['x']= df.index.minute #Extracting minutes
df.plot(x='x', y='some_val')

Upvotes: 1

Related Questions