我们将测量结果(飞行数据)存储在 Pandas 数据帧中。
我们希望能够有一个场“速度”,即速度的 x、y 和 z 分量的向量。比计算速度的范数或计算两个速度的标量积并将结果存储在数据帧的新时间序列中这样的计算容易。
有没有办法用熊猫做到这一点?
例子:
import numpy as np
import numpy.linalg as la
fdo = Store()
df = fdo.getDataFrame(5)
# this works; there are three time series now, that contain the velocity
print df.vx, df.vy, df. vz
# create a vector of velocity vectors
velocities = np.column_stack((df.vx, df.vy, df.vz))
# this does not work:
df['velocities'] = velocities
print "Start calculation!"
# calculate a vector of the norms of this vector (simple method)
norm1 = np.apply_along_axis(la.norm, 1, velocities)
print np.nansum(norm1)
# calculate a vector of the norms of this vector (fast method)
norm2 = np.sum(np.abs(velocities)**2, axis=-1)**(1./2)
print np.nansum(norm2)