例如,给定一个带有1hPeriod
的数据帧,我想设置0&;1当新的5hPeriod
分别开始和结束时,新列中的值
让我们考虑这个输入数据,例如:
import pandas as pd
from random import seed, randint
from collections import OrderedDict
p1h = pd.period_range(start='2020-02-01 00:00', end='2020-03-04 00:00', freq='1h', name='p1h')
seed(1)
values = [randint(0,10) for p in p1h]
df = pd.DataFrame({'Values' : values}, index=p1h)
结果
df.head(10)
Values
p1h
2020-02-01 00:00 2
2020-02-01 01:00 9
2020-02-01 02:00 1
2020-02-01 03:00 4
2020-02-01 04:00 1
2020-02-01 05:00 7
2020-02-01 06:00 7
2020-02-01 07:00 7
2020-02-01 08:00 10
2020-02-01 09:00 6
有没有办法设置一个新的列以获得以下结果? (每个时段的第一行和最后一行分别用0和1初始化)
df['period5h'] = df.resample('5h').???
df.head(10)
Values period5h
p1h
2020-02-01 00:00 2 0 <- 1st row of 5h period
2020-02-01 01:00 9
2020-02-01 02:00 1
2020-02-01 03:00 4
2020-02-01 04:00 1 1 <- last row of 5h period
2020-02-01 05:00 7 0 <- 1st row of 5h period
2020-02-01 06:00 7
2020-02-01 07:00 7
2020-02-01 08:00 10
2020-02-01 09:00 6 1 <- last row of 5h period
请问,这可以通过熊猫的一些功能来实现吗
最终目标是通过0和1之间的线性插值来填充空值,以便获得相对于5h周期的当前行的%进度
另一种方法是使用5h
PeriodIndex初始化第二个数据帧,将新列的值初始化为1
,然后将PeriodIndex
向上采样回1H
以合并两个数据帧
移位(-1)将初始化时段的最后一行
我将重复该过程,而不对值0进行移位
那么,如何创建这个新的数据帧,以便将其合并到第一个数据帧?我尝试了一些合并命令,但有一个错误指出两个索引的频率不同
谢谢你的帮助!胜过
虽然不是最具python风格的方法,但它很有效
时间:8.770591259002686
方法2:
时间:0.009400367736816406
好的,我最终设置为使用以下方法,该方法相当快(无循环)
结果
最好的
使用重采样对象的
indices
属性查找组的第一个和最后一个索引。即使数据没有固定的频率,或者没有完全划分重采样频率的频率,这也会起作用。组将只有一个度量get设置为1
,而不是0
。然后我们相应地设置值相关问题 更多 >
编程相关推荐