gpt4 book ai didi

python - 使用现有的公共(public)假期和周末专栏创建长周末新专栏

转载 作者:行者123 更新时间:2023-12-01 00:35:01 26 4
gpt4 key购买 nike

我有一个 pandas 数据框,其中包含日期、week_day、public_holiday 和周末列。

 weekday Date           Public_Holiday? Weekend?
5 2015-01-10 no yes
0 2015-01-12 no no
1 2015-01-13 no no
2 2015-01-14 no no
3 2015-01-15 no no
4 2015-01-16 no no
5 2015-01-17 no yes
6 2015-01-18 no yes
0 2015-01-19 no no
1 2015-01-20 no no
2 2015-01-21 no no
3 2015-01-22 no no
4 2015-01-23 yes no
5 2015-01-24 no yes
6 2015-01-25 no yes
1 2015-01-27 no no
2 2015-01-28 no no
3 2015-01-29 no no
4 2015-01-30 no no
5 2015-01-31 no yes
0 2015-02-02 no no
1 2015-02-03 no no
2 2015-02-04 no no
3 2015-02-05 no no
4 2015-02-06 no no
5 2015-02-07 no yes
6 2015-02-08 no yes
0 2015-02-09 yes no
1 2015-02-10 no no
2 2015-02-11 no no

我需要添加一个带有长周末标志的附加列。输出应如下所示。

    long_weekend  weekday   Date          Public_Holiday? Weekend?
0 5 2015-01-10 no yes
0 0 2015-01-12 no no
0 1 2015-01-13 no no
0 2 2015-01-14 no no
0 3 2015-01-15 no no
0 4 2015-01-16 no no
0 5 2015-01-17 no yes
0 6 2015-01-18 no yes
0 0 2015-01-19 no no
0 1 2015-01-20 no no
0 2 2015-01-21 no no
0 3 2015-01-22 no no
1 4 2015-01-23 yes no
1 5 2015-01-24 no yes
1 6 2015-01-25 no yes
0 1 2015-01-27 no no
0 2 2015-01-28 no no
0 3 2015-01-29 no no
0 4 2015-01-30 no no
0 5 2015-01-31 no yes
0 0 2015-02-02 no no
0 1 2015-02-03 no no
0 2 2015-02-04 no no
0 3 2015-02-05 no no
0 4 2015-02-06 no no
1 5 2015-02-07 no yes
1 6 2015-02-08 no yes
1 0 2015-02-09 yes no
0 1 2015-02-10 no no
0 2 2015-02-11 no no

常规周末不被视为长周末。仅当周五或周一以及在某些情况下周四或周二是假期时,整个系列才被视为长周末。

这是我在下面尝试过的

df['long_weekend'] = np.where((df['Public_Holiday?'] == 'yes') | (df['Weekend?'] == 'yes'), 1, 0)
df['weekday'] = df['Predicted_Date'].dt.dayofweek
df['long_weekend'] = np.where(((df['long_weekend'] == 1) & (df['weekday'] == 4)) | (df['long_weekend'] == 1) & (df['weekday'] == 0)), 'yes','no')

这给了我以下输出,其中甚至将常规工作日设置为 1。

    long_weekend  weekday         Date   Public_Holiday? Weekend?
1 5 2015-01-10 no yes
0 0 2015-01-12 no no
0 1 2015-01-13 no no
0 2 2015-01-14 no no
0 3 2015-01-15 no no
0 4 2015-01-16 no no
1 5 2015-01-17 no yes
1 6 2015-01-18 no yes
0 0 2015-01-19 no no
0 1 2015-01-20 no no
0 2 2015-01-21 no no
0 3 2015-01-22 no no
1 4 2015-01-23 yes no
1 5 2015-01-24 no yes
1 6 2015-01-25 no yes
0 1 2015-01-27 no no
0 2 2015-01-28 no no
0 3 2015-01-29 no no
0 4 2015-01-30 no no
1 5 2015-01-31 no yes
0 0 2015-02-02 no no
0 1 2015-02-03 no no
0 2 2015-02-04 no no
0 3 2015-02-05 no no
0 4 2015-02-06 no no
1 5 2015-02-07 no yes
1 6 2015-02-08 no yes
1 0 2015-02-09 yes no
0 1 2015-02-10 no no
0 2 2015-02-11 no no

我怎样才能让它工作?任何帮助都会很棒。提前致谢。

最佳答案

想法是通过 shiftcumsum 创建连续的组,并使用 mapvalue_counts 计算组的数量,过滤更多内容为2:

long = (df['Public_Holiday?'] == 'yes') | (df['Weekend?'] == 'yes')
s = long.ne(long.shift()).cumsum()
df['long_weekend'] = np.where((s.map(s.value_counts()) > 2) & long, 1, 0)
<小时/>
print (df)
weekday Predicted_Date Public_Holiday? Weekend? long_weekend
0 5 2015-01-10 no yes 0
1 0 2015-01-12 no no 0
2 1 2015-01-13 no no 0
3 2 2015-01-14 no no 0
4 3 2015-01-15 no no 0
5 4 2015-01-16 no no 0
6 5 2015-01-17 no yes 0
7 6 2015-01-18 no yes 0
8 0 2015-01-19 no no 0
9 1 2015-01-20 no no 0
10 2 2015-01-21 no no 0
11 3 2015-01-22 no no 0
12 4 2015-01-23 yes no 1
13 5 2015-01-24 no yes 1
14 6 2015-01-25 no yes 1
15 1 2015-01-27 no no 0
16 2 2015-01-28 no no 0
17 3 2015-01-29 no no 0
18 4 2015-01-30 no no 0
19 5 2015-01-31 no yes 0
20 0 2015-02-02 no no 0
21 1 2015-02-03 no no 0
22 2 2015-02-04 no no 0
23 3 2015-02-05 no no 0
24 4 2015-02-06 no no 0
25 5 2015-02-07 no yes 1
26 6 2015-02-08 no yes 1
27 0 2015-02-09 yes no 1
28 1 2015-02-10 no no 0
29 2 2015-02-11 no no 0

关于python - 使用现有的公共(public)假期和周末专栏创建长周末新专栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57865331/

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