gpt4 book ai didi

python - 如何将周中的几天分成 1 周并对其执行功能

转载 作者:行者123 更新时间:2023-12-04 08:35:22 25 4
gpt4 key购买 nike

所以我有这样的股市数据

    Date           Open        High        Low        Close     Weekday

0 2007-09-17 4518.450195 4549.049805 4482.850098 4494.649902 Monday
1 2007-09-18 4494.100098 4551.799805 4481.549805 4546.200195 Tuesday
2 2007-09-19 4550.25 4739.0 4550.25 4732.350098 Wednesday
3 2007-09-20 4734.850098 4760.850098 4721.149902 4747.549805 Thursday
4 2007-09-21 4752.950195 4855.700195 4733.700195 4837.549805 Friday

5 2007-09-24 4837.149902 4941.149902 4837.149902 4932.200195 Monday
6 2007-09-25 4939.100098 4953.899902 4878.149902 4938.850098 Tuesday
7 2007-09-26 4937.600098 4980.850098 4930.350098 4940.5 Wednesday
8 2007-09-27 4942.700195 5016.399902 4942.700195 5000.549805 Thursday
9 2007-09-28 4996.450195 5055.799805 4996.450195 5021.350098 Friday

10 2007-10-01 5021.5 5089.299805 5001.350098 5068.950195 Monday
11 2007-10-03 5069.0 5261.350098 5034.149902 5210.799805 Wednesday
12 2007-10-04 5211.649902 5233.100098 5126.049805 5208.649902 Thursday
13 2007-10-05 5208.149902 5248.549805 5164.5 5185.850098 Friday
(为了更好的视觉表现,我把周五之后的空白放在了一起)
我已在 Weekday 中将日期字符串转换为星期几柱子。我想要做的是我想要找到的每一周,哪一天的开盘价最低,并增加到那一天的变量。所以在示例数据中它应该是
第 1 周(指数 0-4)——周二最低 Open价钱
第 2 周(指数 5-9)——周一最低 Open价钱
第 3 周(指数 10-13)——周一最低 Open价钱
所以它应该打印 Monday -2, Tuesday -1, Wednesday - 0, Thursday -0, Friday -0我怎样才能做到这一点?

最佳答案

好像你想groupby每 5 个 obs -

df.groupby(df.index // 5)['Open'].min()

0 4494.100098
1 4837.149902
2 5021.500000
Name: Open, dtype: float64
获取每周最低开盘价的星期几 -
df.loc[df.groupby(df.index // 5)['Open'].idxmin(),'Weekday']

1 Tuesday
5 Monday
10 Monday
Name: Weekday, dtype: object
获取月数 -
df.loc[df.groupby(df.index // 5)['Open'].idxmin(),'Weekday'].value_counts()

Monday 2
Tuesday 1
Name: Weekday, dtype: int64
更新
要处理缺失的天数,请利用 Date要分组的列 -
df.loc[df.groupby(df['Date'].dt.week)['Open'].idxmin(),'Weekday'].value_counts()

Monday 2
Tuesday 1
Name: Weekday, dtype: int64

关于python - 如何将周中的几天分成 1 周并对其执行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64825344/

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