我有一个包含以下信息的数据框:
ticker date close gap
0 BHP 1981-07-31 0.945416 -0.199458
1 BHP 1981-08-31 0.919463 -0.235930
2 BHP 1981-09-30 0.760040 -0.434985
3 BHP 1981-10-30 0.711842 -0.509136
4 BHP 1981-11-30 0.778578 -0.428161
.. ... ... ... ...
460 BHP 2019-11-29 38.230000 0.472563
461 BHP 2019-12-31 38.920000 0.463312
462 BHP 2020-01-31 39.400000 0.459691
463 BHP 2020-02-28 33.600000 0.627567
464 BHP 2020-03-31 28.980000 0.784124
我开发了以下代码来查找行与零交叉时的位置:
zero_crossings =np.where(np.diff(np.sign(BHP_data['gap'])))[0]
这将返回:
array([ 52, 54, 57, 75, 79, 86, 93, 194, 220, 221, 234, 235, 236,
238, 245, 248, 277, 379, 381, 382, 383, 391, 392, 393, 395, 396],
dtype=int64)
我需要执行以下操作:
'gap'
与0
交叉的点之间的月数<12
的项目但是,我不知道如何将这个nd.array
变成有用的东西,我可以从中进行计算。当我尝试时:
pd.DataFrame(zero_crossings)
我得到以下df,它仅返回索引:
0
0 52
1 54
2 57
3 75
4 79
5 86
.. ..
请帮助...
答案 0 :(得分:2)
只需稍微扩展一下代码即可根据需要将zero_crossings
放入原始数据帧。
import pandas as pd
import numpy as np
BHP_data = pd.DataFrame({'gap': [-0.199458, 0.472563, 0.463312, 0.493318, -0.509136, 0.534985, 0.784124]})
BHP_data['zero_crossings'] = 0
zero_crossings = np.where(np.diff(np.sign(BHP_data['gap'])))[0]
print(zero_crossings) # [0 3 4]
# Updates the column to 1 based on the 0 crossing
BHP_data.loc[zero_crossings, 'zero_crossings'] = 1
print(BHP_data)
输出
gap zero_crossings
0 -0.199458 1
1 0.472563 0
2 0.463312 0
3 0.493318 1
4 -0.509136 1
5 0.534985 0
6 0.784124 0