我有以下使用时间戳索引的数据帧结构:
neg neu norm pol pos date
time
1520353341 0.000 1.000 0.0000 0.000000 0.000
1520353342 0.121 0.879 -0.2960 0.347851 0.000
1520353342 0.217 0.783 -0.6124 0.465833 0.000
我从时间戳创建一个日期:
data_frame['date'] = [datetime.datetime.fromtimestamp(d) for d in data_frame.time]
Result:
neg neu norm pol pos date
time
1520353341 0.000 1.000 0.0000 0.000000 0.000 2018-03-06 10:22:21
1520353342 0.121 0.879 -0.2960 0.347851 0.000 2018-03-06 10:22:22
1520353342 0.217 0.783 -0.6124 0.465833 0.000 2018-03-06 10:22:22
我想要按小时分组,同时得到mean for 除时间戳之外的所有值,那应该是小组开始的时间。所以这是我想要存档的结果:
neg neu norm pol pos
time
1520352000 0.027989 0.893233 0.122535 0.221079 0.078779
1520355600 0.028861 0.899321 0.103698 0.209353 0.071811
到目前为止我最接近的是这个answer https://stackoverflow.com/questions/11391969/how-to-group-pandas-dataframe-entries-by-date-in-a-non-unique-column:
data = data.groupby(data.date.dt.hour).mean()
Results:
neg neu norm pol pos
date
0 0.027989 0.893233 0.122535 0.221079 0.078779
1 0.028861 0.899321 0.103698 0.209353 0.071811
但我不知道如何保留考虑到 grouby 开始时间的时间戳。