gpt4 book ai didi

python - 如何按星期一、星期二......对时间序列数据进行分组? Pandas

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:42 29 4
gpt4 key购买 nike

我有时间序列 pandas DataFrame 看起来像

              value
12-01-2014 1
13-01-2014 2
....
01-05-2014 5

我想把它们分组到

1 (Monday, Tuesday, ..., Saturday, Sonday)
2 (Workday, Weekend)

我怎样才能在 pandas 中实现它?

最佳答案

确保您的日期列是日期时间对象并使用日期时间属性:

df = pd.DataFrame({'dates':['1/1/15','1/2/15','1/3/15','1/4/15','1/5/15','1/6/15',
'1/7/15','1/8/15','1/9/15','1/10/15','1/11/15','1/12/15'],
'values':[1,2,3,4,5,1,2,3,1,2,3,4]})
df['dates'] = pd.to_datetime(df['dates'])
df['dayofweek'] = df['dates'].apply(lambda x: x.dayofweek)

dates values dayofweek
0 2015-01-01 1 3
1 2015-01-02 2 4
2 2015-01-03 3 5
3 2015-01-04 4 6
4 2015-01-05 5 0
5 2015-01-06 1 1
6 2015-01-07 2 2
7 2015-01-08 3 3
8 2015-01-09 1 4
9 2015-01-10 2 5
10 2015-01-11 3 6
11 2015-01-12 4 0

df.groupby(df['dates'].apply(lambda x: x.dayofweek)).sum()

df.groupby(df['dates'].apply(lambda x: 0 if x.dayofweek in [5,6] else 1)).sum()

输出:

In [1]: df.groupby(df['dates'].apply(lambda x: x.dayofweek)).sum()
Out[1]:
values
dates
0 9
1 1
2 2
3 4
4 3
5 5
6 7

In [2]: df.groupby(df['dates'].apply(lambda x: 0 if x.dayofweek in [5,6] else 1)).sum()
Out[2]:
values
dates
0 12
1 19

关于python - 如何按星期一、星期二......对时间序列数据进行分组? Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28729710/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com