我有一个2d-Numpy数组,基本上包含一个标签值对。我已将这些基质中的几个结合起来,但我希望将标签四舍五入到小数点后4位并对这些值求和,这样:
[[70.00103, 1],
[70.02474, 1],
[70.02474, 1],
[70.024751, 1],
[71.009100, 1],
[79.0152, 1],
[79.0152633, 1],
[79.0152634, 1]]
变为
[[70.001, 1],
[70.0247, 2],
[70.0248, 1],
[71.0091, 1],
[79.0152, 1],
[79.0153, 2]]
关于如何使用numpy或pandas快速完成此任务的任何想法?谢谢!
答案 0 :(得分:1)
在[10]中:
var lastY = 0;
var render = function () {
if (bgReady)
ctx.drawImage(bgImage, 0, 0);
if (carReady)
ctx.drawImage(carImage, car.x, car.y, (carImage.width / 100 * 50), (carImage.height / 100 * 50));
var deltaY = car.y - lastY;
carImage.width = carImage.width - deltaY;
carImage.height = carImage.height - deltaY;
lastY = car.y;
};
输出[10]:
import numpy as np
x=np.array([[70.00103, 1],[70.02474, 1],[70.02474, 1],[70.024751, 1],[71.009100, 1],[79.0152, 1],[79.0152633, 1],[79.0152634,1]])
x[:,0]=x[:,0].round(4)
x
在[14]中:
array([[ 70.001 , 1. ],
[ 70.0247, 1. ],
[ 70.0247, 1. ],
[ 70.0248, 1. ],
[ 71.0091, 1. ],
[ 79.0152, 1. ],
[ 79.0153, 1. ],
[ 79.0153, 1. ]])
缺货[14]:
import pandas as pd
pd.DataFrame(x).groupby(0).sum()
答案 1 :(得分:0)
这是np.around
的用途:
>>> A=np.array([[70.00103, 1],
... [70.02474, 1],
... [70.02474, 1],
... [70.024751, 1],
... [71.009100, 1],
... [79.0152, 1],
... [79.0152633, 1],
... [79.0152634, 1]])
>>>
>>> np.around(A, decimals=4)
array([[ 70.001 , 1. ],
[ 70.0247, 1. ],
[ 70.0247, 1. ],
[ 70.0248, 1. ],
[ 71.0091, 1. ],
[ 79.0152, 1. ],
[ 79.0153, 1. ],
[ 79.0153, 1. ]])