gpt4 book ai didi

python - 如何使用循环从 Pandas 数据框中获取值?

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

我想循环遍历 startDayValStrendDayValStrstartTimeValStrendTimeValStr 中的每个值并使用值作为 URL 字符串中的参数。我使用所有其他值作为我的 end 变量,即 06:00:00startTimeValStr 值,07:00: 00 是一个 endTimeValStr 值。

如何正确构建循环以在 time_param 中使用这些变量?

我希望 time_param 看起来像这样 ?start=05-05-2019T06:00:00Z&end=05-05-2019T07:00:00Z

这是我当前的代码:

import pandas as pd

rng = pd.date_range(pd.Timestamp('2019-05-05' + ' ' + "'06:00:00'"),periods=25, freq='H')

dtSeries = pd.Series(rng.format())

ddf = dtSeries.to_frame(name='Date')
ddf['time'] = pd.to_datetime(ddf['Date'])
dateDF = ddf['dates'] = ddf['time'].dt.date
timeDF = ddf['dates'] = ddf['time'].dt.time

startDayVal= dateDF[::2]
endDayVal = dateDF[1::2]

startTimeVal= timeDF[::2]
endTimeVal = timeDF[1::2]

startDayValStr = (startDayVal.to_string())
endDayValStr =(endDayVal.to_string())

startTimeValStr = (startTimeVal.to_string())
endTimeValStr = (endTimeVal.to_string())

for startDate, endDate, startTime, endTime in zip (startDayValStr, endDayValStr, startTimeValStr, endTimeValStr):
time_param = '?start='+ startDate +'T'+startTime + 'Z' + '&end='+ endDate + endTime + 'Z'
print time_param

最佳答案

如果使用 Python 3.x,请尝试 string.format方法,用 date.strftime :

rng = pd.date_range(pd.Timestamp('2019-05-05' + ' ' + "'06:00:00'"),
periods=25, freq='H')

for start, end in zip(rng[::2], rng[1::2]):
time_param = '?start={}&end={}'.format(start.strftime('%d-%m-%YT%H:%M:%SZ'),
end.strftime('%d-%m-%YT%H:%M:%SZ'))
print(time_param)

[输出]

?start=05-05-2019T06:00:00Z&end=05-05-2019T07:00:00Z
?start=05-05-2019T08:00:00Z&end=05-05-2019T09:00:00Z
?start=05-05-2019T10:00:00Z&end=05-05-2019T11:00:00Z
?start=05-05-2019T12:00:00Z&end=05-05-2019T13:00:00Z
?start=05-05-2019T14:00:00Z&end=05-05-2019T15:00:00Z
?start=05-05-2019T16:00:00Z&end=05-05-2019T17:00:00Z
?start=05-05-2019T18:00:00Z&end=05-05-2019T19:00:00Z
?start=05-05-2019T20:00:00Z&end=05-05-2019T21:00:00Z
?start=05-05-2019T22:00:00Z&end=05-05-2019T23:00:00Z
?start=06-05-2019T00:00:00Z&end=06-05-2019T01:00:00Z
?start=06-05-2019T02:00:00Z&end=06-05-2019T03:00:00Z
?start=06-05-2019T04:00:00Z&end=06-05-2019T05:00:00Z

关于python - 如何使用循环从 Pandas 数据框中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007553/

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