将数学方程式(dtype 为对象)转换为数字 python

Turn mathematical equations (dtype is object) into a number python

所以我有一列是 dtype 的对象。数据如下所示:

NaN
1*5+4
1*5+3.25
2*5+3.25
4.25
0
2*5+2.5

所需的输出将是:

*blank or 0 for NaN*
9
8.25
13.25
4.25
0
12.5

我试过使用 eval() 和 pd.eval() 但没有成功。我收到以下错误,分别为 "TypeError: eval() arg 1 must be a string, bytes or code object" 和 "AttributeError: 'PandasExprVisitor' object has no attribute 'visit_Ellipsis'"。任何可以执行此操作的功能?还是需要在计算前对列进行操作?

你可以试试这个。如果要将缺失值转换为零,请调用 .fillna(0)

df[0].apply(pd.eval)

#0      NaN
#1     9.00
#2     8.25
#3    13.25
#4     4.25
#5     0.00
#6    12.50
#Name: 0, dtype: float64