简体   繁体   中英

Plotting time series data by grouping at an hour and day level

I have a pandas dataframe which has time stamp when a ride request is raised. The data is of time series nature. I want to plot the no.of requests raised per day over 365 days. I created a new column with all ones and tried group by operation and plot, but no luck. Can anyone please help?

Time Stamp                        Ride
2018-04-07 07:07:17                1
2018-04-07 07:06:12                1

There's a couple ways you can do this. First let's get the data:

import pandas as pd
df = pd.DataFrame({'Time Stamp': ['2018-04-07 07:07:17', '2018-04-07 07:06:12'], 'Ride': [1, 1]})
df['Time Stamp'] = pd.to_datetime(df['Time Stamp'])

You can use groupby with a Grouper , which returns a series:

df.groupby(pd.Grouper(key='Time Stamp', freq='1D')).Ride.sum()

# Time Stamp
# 2018-04-07    2
# Freq: D, Name: Ride, dtype: int64

Or you can put the timestamp in the index and use resample , which returns a dataframe:

df.set_index('Time Stamp').resample('1D').sum()

#               Ride
# Time Stamp    
# 2018-04-07    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM