Python 3.x 计算每个聚合数据时间的值
我试图按小时累计datetime并计算值 以下是示例数据帧:Python 3.x 计算每个聚合数据时间的值,python-3.x,pandas,datetime,Python 3.x,Pandas,Datetime,我试图按小时累计datetime并计算值 以下是示例数据帧: date_time 1/1/10 18:28:54 +0100 #format %d/%b/%Y %H:%M:%S %z 1/1/10 18:29:12 +0100 1/1/10 19:27:50 +0100 1/2/10 20:25:06 +0100 我需要这个: date_time count 1/1/10 18 2 1/1/10 19 1 1/2/10 20 1 这是我的代码: times
date_time
1/1/10 18:28:54 +0100 #format %d/%b/%Y %H:%M:%S %z
1/1/10 18:29:12 +0100
1/1/10 19:27:50 +0100
1/2/10 20:25:06 +0100
我需要这个:
date_time count
1/1/10 18 2
1/1/10 19 1
1/2/10 20 1
这是我的代码:
times = pd.DatetimeIndex(df_3xx.date_time)
df_3xx = df_3xx.groupby([times.hour]).count()
我所拥有的:
ValueError: Array must be all same time zone
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
非常感谢我们可以尝试:
new_df = (df.assign(date_time = df['date_time'].astype(str).str[:9])
.groupby('date_time').size().reset_index(name='count'))
print(new_df)
输出
date_time count
0 1/1/10 18 2
1 1/1/10 19 1
2 1/2/10 20 1
我们可以尝试:
new_df = (df.assign(date_time = df['date_time'].astype(str).str[:9])
.groupby('date_time').size().reset_index(name='count'))
print(new_df)
输出
date_time count
0 1/1/10 18 2
1 1/1/10 19 1
2 1/2/10 20 1
没有显示小时数,str[:9]真的可靠吗?然后我可以使用str[:13]str:9显示到小时,str:13将显示到分钟,你可以在它显示的数据帧中看到它,但是如果在任何情况下你需要按分钟分组,你也可以使用str[:13]没有显示的小时,str[:9]真的可靠吗?然后我可以使用str[:13]str:9显示到小时,str:13显示到分钟,你可以在它显示的数据框中看到它,但是如果在任何情况下你需要按分钟分组,你也可以使用str[:13]