我有一个包含2列的数据框,其中填充了时间戳及其对应的值。 df大约有7000行(2个月的数据),我想每周将它们分成较小的数据帧。我当然可以将它们分成几天等等,但是我不知道几周。数据框如下所示:
2018-08-29T00:03:09 12310
2018-08-29T00:08:10 21231
2018-08-29T00:13:10 19.6
2018-08-29T00:18:10 19321
2018-08-29T00:23:10 182134
2018-08-29T00:28:10 172319
2018-08-29T00:33:10 1734
2018-08-29T00:38:10 1764
2018-08-29T00:43:10 169743
2018-08-29T00:48:10 16747
2018-08-29T00:53:10 17830
谢谢大家提前!
答案 0 :(得分:3)
您可以执行以下操作:
df['Week'] = df['timestamp'].dt.to_period('W-MON')
您将在数据框中获得一个星期列。然后,如果您想浏览几周:
for w, data in df.groupby('Week'):
print(w, data)
您还可以像这样查询包含某些天的星期:
df[df['Week']=='2018-08-29']
答案 1 :(得分:3)
您可以执行以下操作:
考虑df以下:
In [2712]: df
Out[2712]:
Date Value
0 2018-08-29 00:03:09 12,310.00
1 2018-08-29 00:08:10 21,231.00
2 2018-08-29 00:13:10 19.60
3 2018-08-29 00:18:10 19,321.00
4 2018-08-29 00:23:10 182,134.00
5 2018-08-29 00:28:10 172,319.00
6 2018-08-29 00:33:10 1,734.00
7 2018-08-29 00:38:10 1,764.00
8 2018-08-29 00:43:10 169,743.00
9 2018-08-29 00:48:10 16,747.00
10 2018-08-29 00:53:10 17,830.00
In [2711]: df['Date'] = pd.to_datetime(df['Date'])
In [2714]: df['week'] = df['Date'].dt.week
In [2715]: df
Out[2715]:
Date Value week
0 2018-08-29 00:03:09 12,310.00 35
1 2018-08-29 00:08:10 21,231.00 35
2 2018-08-29 00:13:10 19.60 35
3 2018-08-29 00:18:10 19,321.00 35
4 2018-08-29 00:23:10 182,134.00 35
5 2018-08-29 00:28:10 172,319.00 35
6 2018-08-29 00:33:10 1,734.00 35
7 2018-08-29 00:38:10 1,764.00 35
8 2018-08-29 00:43:10 169,743.00 35
9 2018-08-29 00:48:10 16,747.00 35
10 2018-08-29 00:53:10 17,830.00 35
现在将df分组到week
。
In [2717]: grp = df.groupby('week')
In [2721]: for x,y in grp:
...: print(x,y)
...:
35 Date Value week
0 2018-08-29 00:03:09 12,310.00 35
1 2018-08-29 00:08:10 21,231.00 35
2 2018-08-29 00:13:10 19.60 35
3 2018-08-29 00:18:10 19,321.00 35
4 2018-08-29 00:23:10 182,134.00 35
5 2018-08-29 00:28:10 172,319.00 35
6 2018-08-29 00:33:10 1,734.00 35
7 2018-08-29 00:38:10 1,764.00 35
8 2018-08-29 00:43:10 169,743.00 35
9 2018-08-29 00:48:10 16,747.00 35
10 2018-08-29 00:53:10 17,830.00 35
这将为您提供每周较小的数据帧。
答案 2 :(得分:3)