gpt4 book ai didi

python - 进行迭代时如何在 Pandas 中选择工作日/小时

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:45 33 4
gpt4 key购买 nike

我按小时和工作日过滤数据框:

if type == 'daily':
hour = data.index.hour
day = data.index.weekday
selector = ((hour != 17)) | ((day!=5) & (day!=6))
data = data[selector]

if type == 'weekly':
day = data.index.weekday
selector = ((day!=5) & (day!=6))
data = data[selector]

然后我使用 for 需要根据工作日/小时编写一些条件,而 row.index 没有任何信息。在这种情况下我能做什么?

我需要做类似的事情(因为 row.index 没有工作日或小时信息,所以它不会工作):

for index, row in data.iterrows():
if type == 'weekly' & row.index.weekday == 1 & row.index.hour == 0 & row.index.min == 0 | \
type == 'daily' & row.index.hour == 18 & row.index.min == 0:

提前致谢

最佳答案

我知道这不是最优雅的方式,但您可以在列中创建变量:

df['Hour'] = df.index.hour

如果您需要基于这些变量的最小值或最大值,您可以创建另一列并使用 rolling_min 或滚动类型公式。

一旦您有了自己的列,您就可以按照您建议的迭代方式进行迭代。

有关于 index properties here 的信息

关于python - 进行迭代时如何在 Pandas 中选择工作日/小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26717723/

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