【问题标题】:Pandas - pd.read_html, Problems reading negative valuesPandas - pd.read_html,读取负值时出现问题
【发布时间】:2016-02-06 15:23:09
【问题描述】:

我正在尝试将此表转换为 pandas DataFrame。我的问题是熊猫无法识别表中的负值。

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)

有什么建议可以继续吗?

提前谢谢你

【问题讨论】:

  • 我不确定您所说的“识别负值”是什么意思?当我运行你的代码时,我在输出中得到了正值和负值。

标签: python pandas


【解决方案1】:

该表使用不同的hyphen character 而不是ASCII 减号。你可以做这样的事情来替换并重新转换为浮点数。

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

【讨论】:

  • 谢谢你,克里斯布。现在效果很好!
猜你喜欢
  • 2023-04-05
  • 2019-02-10
  • 2021-04-09
  • 2018-02-28
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
相关资源
最近更新 更多