将字符串转换为浮点数以用于 pandas.core.series.Series

时间:2021-03-16 02:58:47

标签: python pandas integer series

我正在尝试将以下 pandas.core.series.Series 转换为浮点数。 代码:

from yahoo_fin.options import get_calls
ticker = 'aapl'
ticker_calls = get_calls(ticker)
df = ticker_calls['Implied Volatility']
con = pd.Series(df).astype('float')

也尝试过:

df.astype('float')

输出:

could not convert string to float: '733.59%'

当只是打印 series/df 数据出现:

 1. 733.59%
 2. 657.03%
 3. 633.79%

但是我无法对此进行任何统计/数学运算。最终目标是能够做到:

(df['Risk_free'] + df['IV'])

输出为:intfloat

我想要完成的示例:

['Risk_free']0.15 + ['IV']7.3359 = ['new col']7.35089

我现在得到的输出:

 4. 1.500%733.59%
 5. 1.500%657.03%

1 个答案:

答案 0 :(得分:0)

你可以去掉'%'符号..

from yahoo_fin.options import get_calls
ticker = 'aapl'
ticker_calls = get_calls(ticker)
df = ticker_calls['Implied Volatility']
con = df.str.rstrip('%').astype(float)

示例:

df = pd.Series(['12%', '12.5%', '35%', '50%', '88.880%'])
df.str.rstrip('%').astype(float)
0    12.00
1    12.50
2    35.00
3    50.00
4    88.88
dtype: float64
相关问题