【问题标题】:Why are digits being omited from a column when importing a data file with Pandas [duplicate]为什么在使用 Pandas 导入数据文件时从列中省略数字 [重复]
【发布时间】:2019-12-12 00:48:59
【问题描述】:

我正在尝试从带有熊猫的 csv 格式的 github 站点导入数据。它似乎工作正常,除了“ZIP”列没有导入所有数字。邮政编码应该有 5 位数字,但似乎省略了前面的 1 或 2 位数字。为什么?!

我要导入this数据:

coords=pd.read_csv('https://gist.githubusercontent.com/erichurst/7882666/raw/5bdc46db47d9515269ab12ed6fb2850377fd869e/US%2520Zip%2520Codes%2520from%25202013%2520Government%2520Data')
coords.head(5)

由于某种原因,它看起来像这样,但 zip 应该是 00601

    ZIP     LAT         LNG
0   601     18.180555   -66.749961

【问题讨论】:

  • @DavidBuck Nice,以及 Wes McKinney 本人的回答 ;)

标签: python pandas numpy


【解决方案1】:

原因是因为 pandas 会自动推断您的列的 dtype 并最终为 ZIP 列分配 integer dtype,因为它仅由数字组成。

您必须明确声明它们是字符串,否则 00601 将只是 601

您可以通过在read_csv 中使用dtypes 参数来做到这一点

pd.read_csv(file, dtype={'ZIP': str})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2017-06-12
    • 2021-06-20
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多