gpt4 book ai didi

python - 迭代索引并将每个范围定义为一天

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

我的最后一个问题没有找到任何帮助/答案,我找到了另一种方法,我想知道是否可以迭代设置为索引的列(具有 pandas 格式的日期时间:

  • 2019-05-02 00:03:00
  • 2019-05-02 00:08:00
  • 2019-05-02 00:13:00
  • 2019-05-02 00:18:00
  • 2019-05-02 00:23:00
  • 2019-05-02 00:28:00
  • 2019-05-02 00:33:00
  • ...
  • )

,这样在迭代期间我可以指定从 00:03:00 到 23:59:00 的范围是一天(做某事)等等。我在处理 pandas 上的日期对象时遇到问题。感谢您的帮助。

最佳答案

您可以像这样在 datetimeday 上执行循环。

import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(columns=['SomeDatetime'], index=[0,1,2,3,4,5])
now = datetime.now()
df.loc[0, 'SomeDatetime'] = now + timedelta(minutes = 10)
df.loc[1, 'SomeDatetime'] = now - timedelta(days = 1)
df.loc[2, 'SomeDatetime'] = now + timedelta(minutes = 15)
df.loc[3, 'SomeDatetime'] = now + timedelta(minutes = 20)
df.loc[4, 'SomeDatetime'] = now + timedelta(minutes = 50)
df.loc[5, 'SomeDatetime'] = now + timedelta(minutes = 90)

print("Dataframe\n")
print(df)
print("\nDay Loop\n")
for day in df['SomeDatetime'].dt.day.unique():
day_value = df[df['SomeDatetime'].dt.day == day]
print(day_value)

这将为您提供以下结果:

Dataframe

SomeDatetime
0 2019-11-15 09:02:47.784752
1 2019-11-14 08:52:47.784752
2 2019-11-15 09:07:47.784752
3 2019-11-15 09:12:47.784752
4 2019-11-15 09:42:47.784752
5 2019-11-15 10:22:47.784752

Day Loop

SomeDatetime
0 2019-11-15 09:02:47.784752
2 2019-11-15 09:07:47.784752
3 2019-11-15 09:12:47.784752
4 2019-11-15 09:42:47.784752
5 2019-11-15 10:22:47.784752
SomeDatetime
1 2019-11-14 08:52:47.784752

您也可以在和其他时间内执行此操作。
评论后编辑

import numpy as np
import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(columns=['SomeDatetime', 'x', 'y'], index=[0,1,2,3,4,5])
now = datetime.now()
df.loc[0, 'SomeDatetime'] = now + timedelta(minutes = 10)
df.loc[1, 'SomeDatetime'] = now - timedelta(days = 1)
df.loc[2, 'SomeDatetime'] = now + timedelta(minutes = 15)
df.loc[3, 'SomeDatetime'] = now + timedelta(minutes = 20)
df.loc[4, 'SomeDatetime'] = now + timedelta(minutes = 50)
df.loc[5, 'SomeDatetime'] = now - timedelta(days = 30*4) - timedelta(days = 3)

df['x'] = pd.Series(np.random.randn(6))
df['y'] = pd.Series(np.random.randn(6))

df.set_index('SomeDatetime', inplace=True)

print("Dataframe\n")
print(df)
print("\nDay Loop\n")

for date in df.index.to_series().dt.date.unique():
print(date)
day_value = df[df.index.to_series().dt.date == date]
print(day_value)
print('\n')

结果:

Dataframe

x y
SomeDatetime
2019-11-15 13:38:13.885939 -1.106788 1.496731
2019-11-14 13:28:13.885939 -0.478087 -0.819366
2019-11-15 13:43:13.885939 1.985328 0.841495
2019-11-15 13:48:13.885939 0.915070 -0.065473
2019-11-15 14:18:13.885939 -0.726703 0.404896
2019-07-15 13:28:13.885939 0.669062 -0.543737

Day Loop

2019-11-15
x y
SomeDatetime
2019-11-15 13:38:13.885939 -1.106788 1.496731
2019-11-15 13:43:13.885939 1.985328 0.841495
2019-11-15 13:48:13.885939 0.915070 -0.065473
2019-11-15 14:18:13.885939 -0.726703 0.404896


2019-11-14
x y
SomeDatetime
2019-11-14 13:28:13.885939 -0.478087 -0.819366


2019-07-15
x y
SomeDatetime
2019-07-15 13:28:13.885939 0.669062 -0.543737


这有帮助吗?

关于python - 迭代索引并将每个范围定义为一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872052/

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