gpt4 book ai didi

pandas - 如何在 Pandas 时间序列中存储向量?

转载 作者:行者123 更新时间:2023-12-04 04:43:44 25 4
gpt4 key购买 nike

我们将测量结果(飞行数据)存储在 Pandas 数据帧中。

我们希望能够有一个“速度”场,它是
速度的 x、y 和 z 分量。比做计算更容易
比如计算速度的范数,或者计算标量积
两个速度并将结果存储在数据帧的新时间序列中。

有没有办法用 Pandas 做到这一点?

示例:

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)

最佳答案

使用 df.apply在过滤列上。

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

如果您要从 Pandas 数据帧中创建 numpy 数组,那么您可能很难做到这一点。 :-)

关于pandas - 如何在 Pandas 时间序列中存储向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com