我有两个数据框列“vibration_X”和“vibration_Y”。此外,我在某些列中几乎没有零值。我想创建新列“theta”,它是vibration_Y 和vibration_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: cannot convert the series to class 'float'
最佳答案
我相信你需要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
关于python - 类型错误 : tan^-1 (tan inverse) of the ratio of two dataframe column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56142253/