Pandaspd.read_html文件,读取负值时出现问题

2024-09-23 00:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将此表转换为pandas数据帧。我的问题是熊猫不认识表中的负值。在

import pandas as pd

url = 'http://www.scb.se/en_/Finding-statistics/Statistics-by-subject-area/Prices-and-Consumption/Consumer-Price-Index/Consumer-Price-Index-CPI/Aktuell-Pong/33779/Consumer-Price-Index-CPI/287612/'

df = pd.read_html(url,index_col='Year',header=0,parse_dates=True)[0]
print(df)

有什么建议吗?在

提前谢谢你


Tags: 数据importhttpurlpandasdfindexconsumer
1条回答
网友
1楼 · 发布于 2024-09-23 00:20:28

该表使用不同的hyphen character,而不是ASCII减号。您可以执行类似的操作来替换并重新转换为float。在

In [64]: df.iloc[0,0]
Out[64]: u'\u20111.1'

In [65]: for column in df:
    ...:     if df[column].dtype == np.object_:
    ...:         df[column] = df[column].str.replace(u'\u2011', '-').astype(float)

In [66]: df.iloc[0,0]
Out[66]: -1.1000000000000001

相关问题 更多 >