如何使用numpy或pandas链接不同大小的列表?

plicqrtu  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(128)

我有一个7个python元素的列表(它总是7个元素,因为它们对应于一周中的7天)。

values = [0.10870379696407702, 0.10279934722899532, 0.07997459688639645, 0.09342230637708339, 0.13035629501918441, 0.1704615636877088, 0.12471332159008049]

字符串
另一方面,我有一周中的7天对应于它们,我将它们存储在元组中(它可以是任何结构)

DAYS = ("2024-01-15", "2024-01-16", "2024-01-17", "2024-01-18", "2024-01-19", "2024-01-20", "2024-01-21")


对于DAYS中的每一天,值中的值对应(以相同的顺序,即对于2024-01-15,对应的值为0.10870379696407702)
它可以像遍历两个结构并将最终字典放在一起一样简单,但问题是我需要YEAR-MONTH-DAY HOUR:CHEMITE:SECOND格式的每个小时的重复值。也就是说,我的最终字典应该是这样的:

{
"2024-01-15 00:00:00": 0.10870379696407702
"2024-01-15 01:00:00": 0.10870379696407702
"2024-01-15 02:00:00": 0.10870379696407702
...
"2024-01-21 23:00:00": 0.12471332159008049
}


我如何才能有效地使用numpy或pandas?我不想在微服务中使用这些库来遍历许多元素。我尝试使用pandas,但最终陷入困境。谢谢。

bqujaahr

bqujaahr1#

可以使用resamplepad填充数据

import pandas as pd

values = [0.10870379696407702, 0.10279934722899532, 0.07997459688639645, 0.09342230637708339, 0.13035629501918441, 0.1704615636877088, 0.12471332159008049]
DAYS = ("2024-01-15", "2024-01-16", "2024-01-17", "2024-01-18", "2024-01-19", "2024-01-20", "2024-01-21")

date_range = pd.date_range(start=DAYS[0], periods=len(DAYS), freq='D')
df = pd.DataFrame({'Date': date_range, 'Value': values})

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

# Resample the DataFrame to fill in missing hours and create the final dictionary
final_dict = df.resample('H').pad().to_dict()['Value']

print(final_dict)

字符串
下面是示例输出:


的数据

{Timestamp('2024-01-15 00:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 01:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 02:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 03:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 04:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 05:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 06:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 07:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 08:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 09:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 10:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 11:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 12:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 13:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 14:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 15:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 16:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 17:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 18:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 19:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 20:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 21:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 22:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-15 23:00:00', freq='H'): 0.10870379696407702, Timestamp('2024-01-16 00:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 01:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 02:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 03:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 04:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 05:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 06:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 07:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 08:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 09:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 10:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 11:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 12:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 13:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 14:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 15:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 16:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 17:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 18:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 19:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 20:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 21:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 22:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-16 23:00:00', freq='H'): 0.10279934722899532, Timestamp('2024-01-17 00:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 01:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 02:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 03:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 04:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 05:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 06:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 07:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 08:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 09:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 10:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 11:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 12:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 13:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 14:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 15:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 16:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 17:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 18:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 19:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 20:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 21:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 22:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-17 23:00:00', freq='H'): 0.07997459688639645, Timestamp('2024-01-18 00:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 01:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 02:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 03:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 04:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 05:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 06:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 07:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 08:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 09:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 10:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 11:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 12:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 13:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 14:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 15:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 16:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 17:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 18:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 19:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 20:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 21:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 22:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-18 23:00:00', freq='H'): 0.09342230637708339, Timestamp('2024-01-19 00:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 01:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 02:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 03:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 04:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 05:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 06:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 07:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 08:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 09:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 10:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 11:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 12:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 13:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 14:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 15:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 16:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 17:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 18:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 19:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 20:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 21:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 22:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-19 23:00:00', freq='H'): 0.13035629501918441, Timestamp('2024-01-20 00:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 01:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 02:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 03:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 04:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 05:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 06:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 07:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 08:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 09:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 10:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 11:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 12:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 13:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 14:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 15:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 16:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 17:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 18:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 19:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 20:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 21:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 22:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-20 23:00:00', freq='H'): 0.1704615636877088, Timestamp('2024-01-21 00:00:00', freq='H'): 0.12471332159008049}


或者你可以使用循环来美化格式:

result_dict = {}

# Iterate through each day and generate hourly timestamps
for day, value in zip(DAYS, values):
    for hour in range(24):
        # Construct the hourly timestamp
        timestamp = f"{day} {hour:02d}:00:00"  # Format: YYYY-MM-DD HH:MM:SS
        result_dict[timestamp] = value
 
print(result_dict)


下面是示例输出:


相关问题