python - 如何以最小间隔将未排序的时间序列数据切割成箱?
问题描述
我有一个这样的数据框
x = pd.DataFrame({'a':[1.1341, 1.13421, 1.13433, 1.13412, 1.13435, 1.13447, 1.13459, 1.13452, 1.13471, 1.1348, 1.13496,1.13474,1.13483,1.1349,1.13502,1.13515,1.13526,1.13512]})
我们如何拆分这个系列以获得以下输出,使得最小差异至少为 0.0005
x['output'] = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0]
解决方案
I don't believe there is a vectorized way to do this, so you probably need to loop through the values.
x = x.assign(output=0) # Initialize all the output values to zero.
x['output'].iat[0] = 1
threshold = 0.0005
prior_val = x['a'].iat[0]
for n, val in enumerate(x['a']):
if abs(val - prior_val) >= threshold:
x['output'].iat[n] = 1
prior_val = val # Reset to new value found that exceeds threshold.
推荐阅读
- mysql - MySQL ERROR 2026 - SSL 连接错误 - Ubuntu 20.04
- mysql - SQL - 从大型数据集中返回多条记录中的最新记录
- php - Laravel 避免子表中的重复条目
- c++ - 需要对未初始化的变量进行一些澄清
- python - 在 Mac 上安装 Tkinter 以在 Pycharm 上使用
- java - 使用 Hive 的 Spark,无法使用 Hive 支持实例化 SparkSession,因为找不到 Hive 类
- mysql - 在列中创建非零天的运行总计
- javascript - 笑话:函数中的模拟类方法
- mule - 错误传播/转换消息组件不会覆盖 Mule4 的默认行为
- python - 如何防止除以零异常?