首页 > 解决方案 > TypeError : tan^-1 (tan inverse) 两个数据框列的比率

问题描述

我有两个数据框列“vibration_X”和“vibration_Y”。此外,某些列中的零值很少。我想创建新列“theta”,它是振动_Y 和振动_X 的 tan 倒数之比。

以下是我的示例数据:

     vibration_Y  vibration_X
0           10            7
1           10            8
2            9            8
3           10           11
4           13            5
5            3            0
6           12            8
7           12            9
8           11           10
9           10           11

以下是我尝试过的代码,但出现错误:

df['theta'] = math.atan(df['vibration_Y']/df['vibration_X'].astype(float))

TypeError:无法将系列转换为“浮点”类

标签: pythonpandasmath

解决方案


我相信你需要numpy.arctan

df['theta'] = np.arctan(df['vibration_Y']/df['vibration_X'])
print (df)
   vibration_Y  vibration_X     theta
0           10            7  0.960070
1           10            8  0.896055
2            9            8  0.844154
3           10           11  0.737815
4           13            5  1.203622
5            3            0  1.570796
6           12            8  0.982794
7           12            9  0.927295
8           11           10  0.832981
9           10           11  0.737815

推荐阅读