2013-08-29 53 views
2

我们将测量结果(飞行数据)存储在熊猫数据框中。如何在熊猫时间序列中存储矢量?

我们希望能够有一个场“速度”,这是速度的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) 

回答

3

上过滤列使用df.apply

velocity = ['vx','vy','vz'] 
norm1 = df[velocity].apply(la.norm, 1) 

如果您是从熊猫数据框中创建numpy数组,那么您可能就是这么做了。 :-)