[英]Can't replace string symbol “-” in dataframe on jupyter notebook
I import data from "url = (" http://finviz.com/quote.ashx?t= " + symbol.lower())" 我从“ URL =(“ http://finviz.com/quote.ashx?t= ” + symbol.lower())“导入数据
and got the table: 并得到表:
P/B P/E Forward P/E PEG Debt/Eq EPS (ttm) Dividend % ROE \
AMZN 18.73 92.45 56.23 2.09 1.21 16.25 - 26.70%
GOOG 4.24 38.86 - 2.55 - 26.65 - -
PG 4.47 22.67 19.47 3.45 0.61 4.05 3.12% 18.80%
KO 11.04 30.26 21.36 4.50 2.45 1.57 3.29% 15.10%
IBM 5.24 9.28 8.17 9.67 2.37 12.25 5.52% 30.90%
ROI EPS Q/Q Insider Own
AMZN 3.50% 1026.20% 16.20%
GOOG - 36.50% 5.74%
PG 13.10% 15.50% 0.10%
KO 12.50% 56.80% 0.10%
IBM 17.40% 0.70% 0.10%
Then I was trying to convert string to float: 然后我试图将字符串转换为浮点数:
df = df[(df['P/E'].astype(float)<20) & (df['P/B'].astype(float) < 3)]
and got "ValueError: could not convert string to float:" 并得到“ ValueError:无法将字符串转换为浮点数:”
I think that values 0.70% and sign "-" is the problem. 我认为值0.70%并用符号“-”表示问题。
I tried: 我试过了:
df.replace("-","0")
df.replace('-', 0)
df.replace('-', nan)
But nothing works. 但是什么都行不通。
You may need to assign it back 您可能需要将其分配回来
df=df.replace("-","0")
And I recommend to_numeric
我建议
to_numeric
df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')
You should use numpy: 您应该使用numpy:
import numpy as np
then the next replacement: 然后下一个替换:
df = df.replace('-', np.nan)
Next, change the datatype: 接下来,更改数据类型:
df = df['Forward P/E'].astype(float)
Lastly, you can test if the datatype is float64. 最后,您可以测试数据类型是否为float64。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.