gpt4 book ai didi

python - 基于每日时间序列数据框创建工作日/周末时间序列数据框

转载 作者:行者123 更新时间:2023-12-01 09:09:02 25 4
gpt4 key购买 nike

例如,我创建了一个包含时间序列信息的数据框

Time      daily-bill
2012-01-01 200
2012-01-02 300
2012-01-03 100
2012-01-04 500
….

我想根据上述时间序列创建另一个时间序列数据框。如何在 Pandas 中做到这一点?

Time(weekday-and-weekend)                       total-bill
Monday-Friday
Weekend
Monday-Friday
Weekend
Monday-Friday
Weekend

换句话说,时间步将是weekdayweekend的连续序列。 工作日周一至周五组成;而周末星期六星期日组成。 total-bill 列将存储相应日期发生的账单总和,这些信息来自现有的时间序列。

最佳答案

用途:

print (df)
Time daily-bill
0 2012-01-01 200
1 2012-01-02 300
2 2012-01-03 100
3 2012-01-04 500
4 2012-01-05 200
5 2012-01-06 300
6 2012-01-07 100
7 2012-01-08 500
8 2012-01-09 500

arr = np.where(df['Time'].dt.weekday > 4, 'Weekend','Monday-Friday')

s = pd.Series(arr)
s1 = s.ne(s.shift()).cumsum()

df = (df['daily-bill'].groupby([s1,s.rename('Time')])
.sum()
.reset_index(level=0, drop=True)
.reset_index())
print (df)
Time daily-bill
0 Weekend 200
1 Monday-Friday 1400
2 Weekend 600
3 Monday-Friday 500

说明:

  1. 首先由 weekday 创建系列numpy.where .
  2. 然后创建另一个由 cumsum 创建的系列移动 s 的次数 shift用于区分连续值
  3. 聚合sum并按 reset_index 删除第一级与 drop=True

详细信息:

print (s)
0 Weekend
1 Monday-Friday
2 Monday-Friday
3 Monday-Friday
4 Monday-Friday
5 Monday-Friday
6 Weekend
7 Weekend
8 Monday-Friday
dtype: object

print (s1)
0 1
1 2
2 2
3 2
4 2
5 2
6 3
7 3
8 4
dtype: int32

编辑:

如果输入DataFrame的第一列是DatetimeIndex:

print (df)
daily-bill
Time
2012-01-01 200
2012-01-02 300
2012-01-03 100
2012-01-04 500
2012-01-05 200
2012-01-06 300
2012-01-07 100
2012-01-08 500
2012-01-09 500

arr = np.where(df.index.weekday > 4, 'Weekend','Monday-Friday')

s = pd.Series(arr, index=df.index)
s1 = s.ne(s.shift()).cumsum()

df = (df['daily-bill'].groupby([s1,s.rename('Time')])
.sum()
.reset_index(level=0, drop=True)
.reset_index())
print (df)
Time daily-bill
0 Weekend 200
1 Monday-Friday 1400
2 Weekend 600
3 Monday-Friday 500

关于python - 基于每日时间序列数据框创建工作日/周末时间序列数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51815172/

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