python - 类型错误 : tan^-1 (tan inverse) of the ratio of two dataframe column

标签 python pandas math

我有两个数据框列“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/

相关文章:

python - 如何使用 Selenium 为 PDF 打印指定打印位置

python - 有效的数据结构可以在 Pandas 中保持按时间键控的类似字典的观察结果?

python - Pandas:使用 "and"中两个series的 `where`

objective-c - 我在 Objective-C 中的矩阵方面遇到问题

c++ - 多边形线碰撞检测

c++ - 汉明立方体的数据结构

python - 替换 MainWindow 中的 CentralWidget

python - 在 numpy 中按索引选择

python - 根据字符串的首选顺序对可迭代对象进行排序

python - 如何优化 pandas 数据框的分块?