导入csv时,pandas无法识别浮点数列 [英] Pandas does not recognise a column of floats when importing a csv

查看:89
本文介绍了导入csv时,pandas无法识别浮点数列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将IMF统计信息读入熊猫数据框:

I am trying to read the IMF statistics into a pandas dataframe:

import pandas as pd
df = pd.read_table("http://www.imf.org/external/pubs/ft/weo/2013/02/weodata/WEOOct2013all.xls",
                   na_values=['n/a','--'],thousands=',')

除一列外,所有列均具有dtype对象:

All the columns, except one, have dtype object:

In [5]: df
Out[5]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 8318 entries, 0 to 8317
Data columns (total 49 columns):
...
dtypes: float64(1), object(48)

我手动检查了文件,但在大多数列中都找不到任何不是数字的值或代码中明确提到的NaN值之一.

I manually inspected the file and could not find any value which is not a numeric one, or one of the NaN values explicitly mentioned in the code, in most columns.

我在Wakari.io的Anaconda 1.5.0上使用Python 2.7.5,numpy 1.7.1和pandas 0.11.0.

推荐答案

正如Jeff所提到的,这是< ; = 0.12 (但在0.13中固定).

As mentioned by Jeff, this was a bug in <=0.12 (but is fixed in 0.13).

In [11]: s = '''A;B
1;2,000
3;4'''

In [12]: pd.read_csv(StringIO(s), sep=';', thousands=',')
Out[12]: 
   A     B
0  1  2000
1  3     4

[2 rows x 2 columns]

In [13]: pd.version.version
Out[13]: '0.13.0rc1-82-g66934c2'

这篇关于导入csv时,pandas无法识别浮点数列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆