我正在尝试根据第二频率(df2)的索引将具有特定值(df1)的时间值范围转置到矩阵中。
目前,我正在遍历第一个数据帧,并使用loc函数将值转置到第二个数据帧中的多行,但是我认为这是一个很慢的方法,因为迭代速度太慢了。
def create_columns(df):
x = list(dict.fromkeys(df['X']))
return x
date_rng = pd.date_range(start=df1.iloc[0]['FROM'], end=df1.iloc[-1]['TO'], freq='s')
cols = create_columns(df1)
df2 = pd.DataFrame(columns = [cols], index = date_rng)
for index, row in df1.iterrows():
date_rng = pd.date_range(start=row['FROM'], end=row['TO'], freq='s')
df2.loc[date_rng, row['X']] = row['Y']
基本df1:
X FROM TO Y
0 A 01/01/2019 00:00:00 01/01/2019 00:00:09 1
1 B 01/01/2019 00:00:05 01/01/2019 00:00:14 1
2 A 01/01/2019 00:00:10 01/01/2019 00:00:30 0
3 B 01/01/2019 00:00:15 01/01/2019 00:00:40 0
4 C 01/01/2019 00:00:00 01/01/2019 00:01:00 0
结果df2:
A B C
01/01/2019 00:00:00 1 1 0
01/01/2019 00:00:01 1 1 0
01/01/2019 00:00:02 1 1 0
01/01/2019 00:00:03 1 1 0
01/01/2019 00:00:04 1 1 0
01/01/2019 00:00:05 1 1 0
01/01/2019 00:00:06 1 1 0
01/01/2019 00:00:07 1 1 0
01/01/2019 00:00:08 1 1 0
01/01/2019 00:00:09 1 1 0
01/01/2019 00:00:10 1 0 0
01/01/2019 00:00:11 1 0 0
01/01/2019 00:00:12 1 0 0