简体   繁体   English

通过在小时和天级别进行分组来绘制时间序列数据

[英]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.我有一个 Pandas 数据框,它在提出乘车请求时带有时间戳。 The data is of time series nature.数据具有时间序列性质。 I want to plot the no.of requests raised per day over 365 days.我想绘制超过 365 天每天提出的请求数。 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:您可以将groupbyGrouper一起使用,它返回一个系列:

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:或者您可以将时间戳放在索引中并使用resample ,它返回一个数据帧:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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