gpt4 book ai didi

python - 在第一次匹配 Pandas 时间序列数据后忽略 np.where

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:33 25 4
gpt4 key购买 nike

按照以下示例,我需要让我的代码在首次匹配时间序列数据后忽略 np.where。

因此,在 2014-03-04 14:00:00 行中,np.where 在 test_output 列上给出了 1.0,正如预期的那样,也在下一行上给出了 1.0。我只希望它触发一次。我将在问题的末尾显示所需的输出。

感谢您查看问题。

为测试生成的数据框:

df = pd.DataFrame(index=pd.date_range(start='2014-03-04 09:00:00', end='2014-03-04 16:15:00', freq='1h') + pd.date_range(start='2014-03-05 09:00:00', end='2014-03-05 16:15:00', freq='1h'), data={'test_1': np.nan})

df['test_1'][5:16]=1.0

df['test_output'] = np.where(df['test_1'] == 1.0,1.0,np.nan);
df

test_1 test_output
2014-03-04 09:00:00 NaN NaN
2014-03-04 10:00:00 NaN NaN
2014-03-04 11:00:00 NaN NaN
2014-03-04 12:00:00 NaN NaN
2014-03-04 13:00:00 NaN NaN
2014-03-04 14:00:00 1.0 1.0
2014-03-04 15:00:00 NaN NaN
2014-03-04 16:00:00 1.0 1.0
2014-03-05 09:00:00 1.0 1.0

这是期望的输出:

test_1  test_output
2014-03-04 09:00:00 NaN NaN
2014-03-04 10:00:00 NaN NaN
2014-03-04 11:00:00 NaN NaN
2014-03-04 12:00:00 NaN NaN
2014-03-04 13:00:00 NaN NaN
2014-03-04 14:00:00 1.0 1.0
2014-03-04 15:00:00 NaN NaN
2014-03-04 16:00:00 1.0 NaN
2014-03-05 09:00:00 1.0 NaN

最佳答案

使用first_valid_index在掩码上设置第一行:

In [30]:
df.loc[df[df['test_1'] == 1.0].first_valid_index(),'test_output'] = 1.0
df

Out[30]:
test_1 test_output
2014-03-04 09:00:00 NaN NaN
2014-03-04 10:00:00 NaN NaN
2014-03-04 11:00:00 NaN NaN
2014-03-04 12:00:00 NaN NaN
2014-03-04 13:00:00 NaN NaN
2014-03-04 14:00:00 1.0 1.0
2014-03-04 15:00:00 1.0 NaN
2014-03-04 16:00:00 1.0 NaN
2014-03-05 09:00:00 1.0 NaN
2014-03-05 10:00:00 1.0 NaN
2014-03-05 11:00:00 1.0 NaN
2014-03-05 12:00:00 1.0 NaN
2014-03-05 13:00:00 1.0 NaN
2014-03-05 14:00:00 1.0 NaN
2014-03-05 15:00:00 1.0 NaN
2014-03-05 16:00:00 1.0 NaN

分解以上内容:

In [32]:
df['test_1'] == 1.0

Out[32]:
2014-03-04 09:00:00 False
2014-03-04 10:00:00 False
2014-03-04 11:00:00 False
2014-03-04 12:00:00 False
2014-03-04 13:00:00 False
2014-03-04 14:00:00 True
2014-03-04 15:00:00 True
2014-03-04 16:00:00 True
2014-03-05 09:00:00 True
2014-03-05 10:00:00 True
2014-03-05 11:00:00 True
2014-03-05 12:00:00 True
2014-03-05 13:00:00 True
2014-03-05 14:00:00 True
2014-03-05 15:00:00 True
2014-03-05 16:00:00 True
Freq: BH, Name: test_1, dtype: bool

In [33]:
df[df['test_1'] == 1.0].first_valid_index()

Out[33]:
Timestamp('2014-03-04 14:00:00', offset='BH')

您可以使用 np.where 通过再次屏蔽 df 来完成此操作,以便通过将 np 数组与 1.0 进行比较来生成 NaN 条件为假的情况:

In [41]:
df.loc[df[np.where(df['test_1'] == 1.0, 1.0, 0) == 1].first_valid_index(), 'test_output'] = 1.0

df
Out[41]:
test_1 test_output
2014-03-04 09:00:00 NaN NaN
2014-03-04 10:00:00 NaN NaN
2014-03-04 11:00:00 NaN NaN
2014-03-04 12:00:00 NaN NaN
2014-03-04 13:00:00 NaN NaN
2014-03-04 14:00:00 1.0 1.0
2014-03-04 15:00:00 1.0 NaN
2014-03-04 16:00:00 1.0 NaN
2014-03-05 09:00:00 1.0 NaN
2014-03-05 10:00:00 1.0 NaN
2014-03-05 11:00:00 1.0 NaN
2014-03-05 12:00:00 1.0 NaN
2014-03-05 13:00:00 1.0 NaN
2014-03-05 14:00:00 1.0 NaN
2014-03-05 15:00:00 1.0 NaN
2014-03-05 16:00:00 1.0 NaN

关于python - 在第一次匹配 Pandas 时间序列数据后忽略 np.where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183182/

25 4 0
文章推荐: python - 尝试使用 webrtcvad 时出错
文章推荐: javascript - 使用 gulp.watch 会抛出 "TypeError: Object # has no method ' watch'"