【问题标题】:Create new column in pandas based on another columns where each cell will be numpy.ndarray根据每个单元格将为 numpy.ndarray 的另一列在 pandas 中创建新列
【发布时间】:2018-05-10 20:15:24
【问题描述】:

我想从欧拉角计算四元数。然后我想将整个四元数值存储在一列中。四元数是 numpy.ndarray。

我发现了类似的问题,但在所有解决方案中,列都存储一维值。

这是我的代码:

import transforms3d

def computeQuat(p,y,r,syscoord):
    return transforms3d.euler.euler2quat(p, y, r, syscoord)

dataClassic['Quaternion'] = (dataClassic.apply(lambda x: computeQuat(x['Pitch_Init'],\
                                        x['Yaw_Init'],\
                                        x['Roll_Init'], 'rxyz'), axis=1))

我得到错误:传递值的形状是 (362, 4),索引暗示 (362, 6)。

如果我更改它返回一个值的函数 computeQuat,它可以正常工作:

def computeQuat(p,y,r,syscoord):
    return transforms3d.euler.euler2quat(p, y, r, syscoord)[0]

但我想将整个四元数作为 numpy.ndarray 存储在一列中。感谢您的帮助。

我使用 python 2。

我的数据框中的一些行:

Pitch_Init  Yaw_Init  Roll_Init        X        Y        Z
0       0.00000  0.000000   0.000000  0.00000  0.00000   0.0000
1      -3.02664 -0.196047   0.027153  1.15756 -3.73556  60.8734
2      -3.04845 -0.180832   0.024738  1.16244 -3.75089  60.9885
3      -3.06983 -0.168351   0.022593  1.16580 -3.76106  61.0987
4      -3.12110 -0.135368   0.021681  1.18352 -3.78380  61.4627
5       3.11304 -0.130364   0.019365  1.17835 -3.77255  61.5189
6       3.09046 -0.137977   0.017496  1.17643 -3.76929  61.4626
7       3.08333 -0.132229   0.017267  1.17355 -3.76809  61.4604
8       3.07844 -0.141410   0.017119  1.17351 -3.76710  61.4747
9       3.09239 -0.106203   0.018702  1.19171 -3.77195  61.5361
10      3.08501 -0.125604   0.017750  1.19317 -3.76052  61.4881

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    新答案:

    def computeQuat(row):
        quat = (row['Pitch_Init'], row['Yaw_Init'], row['Roll_Init'], 'rxyz')
        return transforms3d.euler.euler2quat(*quat)
    
    df['q0'] = 0
    df['q1'] = 0
    df['q2'] = 0
    df['q3'] = 0
    
    for row_num, row in df.iterrows():
        quat_res = computeQuat(row)
        df.loc[row_num, 'q0'] = quat_res[0]
        df.loc[row_num, 'q1'] = quat_res[1]
        df.loc[row_num, 'q2'] = quat_res[2]
        df.loc[row_num, 'q3'] = quat_res[3]
    

    这不是最漂亮的解决方案,但它确实有效。

    【讨论】:

    • 我试过你的解决方案,它给出:('euler2quat() 至少需要 3 个参数(1 个给定)',你'发生在索引 0')
    • 哦,对不起,我正在更正我的答案 - 现在应该是正确的。
    • 现在,我得到:ValueError:传递值的形状是 (362, 4),索引意味着 (362, 6)
    • 应该 transforms3d.euler.euler2quat(*quat) 每行返回单个值还是多个?如果多个尝试将它们返回到新数据帧并将其连接到旧数据帧。告诉我它是否会起作用。
    • transforms3d.euler.euler2quat 返回大小为 4 的 numpy.ndarray 我试过了: dNew = pd.DataFrame() dNew['Quaternion'] = dataClassic.apply(computeQuat, axis=1) 我得到了同样的错误,似乎 dataClassic.apply 不想返回大小为 4 的 numpy.ndarray 列
    猜你喜欢
    • 2022-10-15
    • 2020-04-16
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2021-03-02
    • 2022-09-06
    相关资源
    最近更新 更多