值错误:无法分析位置 1363 处的字符串"-"



似乎我从(API)中提取的数据源有一个奇怪的'-'符号,当我做str.replace时无法识别。下面是我使用的代码和库。在pd.to_numeric上发生错误。转换为float会返回相同的错误,但没有位置。

Y = xy['QPerf'].str.rstrip('%')
Y = Y.str.replace('-', '-')
Y = pd.to_numeric(Y)
Y = Y.apply(lambda x: 1 if x > 0 else 0)
print(Y)

我试过str.encode('UTF-8').str.decode('UTF-8'),但不出所料它不起作用。

这里是库代码,用于获取您自己的数据来尝试。

from finvizfinance.quote import finvizfinance
from finvizfinance.screener.overview import Overview
stock = finvizfinance('TSLA')
stock_fundament = stock.TickerFundament()
qperf = stock_fundament['Perf Quarter']

这将返回一个数据帧。

您始终可以忽略错误,并使用errors='coerce'参数将pd.to_numeric替换为NaNs。这可能也是-的意思,它不是一个数字,它代表丢失的数据。

Y = pd.to_numeric(xy['QPer'].str.rstrip('%'), errors='coerce')

这也有忽略任何其他错误的缺点,并且可能使您错过您想知道的格式错误。

如果你从csv文件中读取,你可以使用na_values来指定-表示NaNs。在这种情况下,我们可以使用.mask()-替换为NaNs,然后使用to_numeric:

Y = pd.to_numeric(xy['QPer'].str.rstrip('%').mask(xy['QPer'] == '-'))

最新更新