Python: math.acos(x) 对于列表

Python: math.acos(x) for list

这是我在列表中应用反余弦的函数

import math

ListAcos = lambda x: math.acos(float(x))

ListAcos(Mylist)

cannot convert the series to <class 'float'>

我的列表是一系列类似下面的列表

ID
25      -0.239509
26      -0.274846
27      -0.316483
28      -0.330328
29      -0.266320

37371    0.628767
37372    0.687516
37373    0.700680
37374    0.731391
37375    0.693018
Length: 36223, dtype: float64

有人知道我应该应用的转换吗?

IIUC 您可以使用 pandas.Series.apply 方法进行转换,因为您可以在方法中插入您的 lambda 函数:

MyList = MyList.apply(ListAcos)

Python 不是 numpy。

标准库函数math.acos仅对浮点标量(即单个值)进行运算。您需要 np.arccos 才能对 numpy 数组进行操作:

np.arccos(MyList.values)

或者如果您希望结果是一个系列:

np.arccos(MyList)