2013-08-29 95 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數組,那麼您可能就是這麼做了。 :-)