说我有一个数组数组如下:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
这是一个价格和数量清单,正数是买入,负数是卖出。
如果目前的市场价格是7050,我想要挤压'我的价格如下:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7050. , 300. ], # Price & quantity changed here
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
并且在另一个方向做同样的事情,所以如果市场价格是7085:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7085. , -300. ], # Price & quantity change here
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
写这样东西最干净的方法是什么?理想情况下,我想将挤压和数量分组写成两个单独的步骤。
答案 0 :(得分:1)
pandas使这种操作更容易。
>>> import numpy as np
>>> import pandas as pd
>>>
>>> t = np.array([[ 7108.4, -600. ],
... [ 7101.3, -500. ],
... [ 7094.3, -400. ],
... [ 7087.2, -300. ],
... [ 7080.1, -200. ],
... [ 7073.1, -100. ],
... [ 7058.3, 100. ],
... [ 7051.3, 200. ],
... [ 7044.2, 300. ],
... [ 7037.1, 400. ],
... [ 7030.1, 500. ],
... [ 7023. , 600. ]])
>>>
>>> df = pd.DataFrame(t, columns=['price', 'quantity'])
>>> (df.price > 7050) & (df.quantity > 0)
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 True
8 False
9 False
10 False
11 False
dtype: bool
>>> df[(df.price > 7050) & (df.quantity > 0)]
price quantity
6 7058.3 100.0
7 7051.3 200.0
>>> df[(df.price > 7050) & (df.quantity > 0)].quantity.sum()
300.0
>>> df[(df.price < 7085) & (df.quantity < 0)]
price quantity
4 7080.1 -200.0
5 7073.1 -100.0
>>> df[(df.price < 7085) & (df.quantity < 0)].quantity.sum()
-300.0
如果你愿意,你仍然可以坚持使用numpy
:
>>> import numpy as np
>>> import pandas as pd
>>>
>>> t = np.array([[ 7108.4, -600. ],
... [ 7101.3, -500. ],
... [ 7094.3, -400. ],
... [ 7087.2, -300. ],
... [ 7080.1, -200. ],
... [ 7073.1, -100. ],
... [ 7058.3, 100. ],
... [ 7051.3, 200. ],
... [ 7044.2, 300. ],
... [ 7037.1, 400. ],
... [ 7030.1, 500. ],
... [ 7023. , 600. ]])
>>>
>>>
>>> t[:,0]
array([ 7108.4, 7101.3, 7094.3, 7087.2, 7080.1, 7073.1, 7058.3,
7051.3, 7044.2, 7037.1, 7030.1, 7023. ])
>>> t[:,0] > 7050
array([ True, True, True, True, True, True, True, True, False,
False, False, False], dtype=bool)
>>> (t[:,0] > 7050) & (t[:,1] > 0)
array([False, False, False, False, False, False, True, True, False,
False, False, False], dtype=bool)
>>> t[(t[:,0] > 7050) & (t[:,1] > 0)]
array([[ 7058.3, 100. ],
[ 7051.3, 200. ]])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)]
array([[ 7080.1, -200. ],
[ 7073.1, -100. ]])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)][:,1]
array([-200., -100.])
>>> t[(t[:,0] < 7085) & (t[:,1] < 0)][:,1].sum()
-300.0
答案 1 :(得分:0)
因为Eric的答案很棒,但根据你的评论:
是否可以修改原始数组,以便输出为 包含内部变更的订单的完整列表?
这是我的方法,没有numpy:
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7050
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0]>positive_value:
if item[1]>0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
输出:
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7080.1, -200.0], [7073.1, -100.0], [7050, 300.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]
第二个:
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7085
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0] < positive_value:
if item[1] < 0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
输出:
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7085, -300.0], [7058.3, 100.0], [7051.3, 200.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]