panda数据帧中向量处理的方便函数
pandas-vectors的Python项目详细描述
这些是一组方便的函数,有助于使用 存储在pandas数据帧中的向量。
例如,如果您有一个列为my_vector_x的数据帧, my_vector_y和my_vector_z然后您发现自己编写的代码类似于 这个:
forvectorin['my_vector_x','my_vector_y','my_vector_z']:df[vector[:-2]+'_new'+vector[-2:]]=func(df[vector])
现在,您可以编写:
importpandas_vectorsaspvforvector,newinzip(pv.indexer('my_vector'),pv.indexer('my_vector_new')):df[new]=func(df[vector])
实际上,您可以简化它:
df=pv.transform(df,'my_vector','_new',func)
所有以向量作为输入的函数都有一个向量列表。
df=pv.magnitude(df,['my_vector','my_new_vector'])
以df作为第一个参数的函数返回修改后的df。
不使用_x、_y和_z作为向量名?没问题。
# Set the vector suffixes to the argument givenpv.set_vectornames(['_u','_v','_w'])# There are also some builtin shortcutspv.set_vectornames('xy')# ['_x', '_y']pv.set_vectornames('xyz')# ['_x', '_y', '_z']pv.set_vectornames('pyr')# ['_p', '_y', '_r']pv.set_vectornames('PYR')# ['_pitch', '_yaw', '_roll']
也可以使用with:
pv.set_vectornames('xyz')withpv.vectornames('xy'):df=pv.magnitude(df,'my_vector','_magxy')# only xy magnitudedf=pv.magnitude(df,'my_vector','_mag')# xyz magnitude
更改日志
0.1.1函数/变量错误修复 0.1初始版本