python read_fwf错误:"python-fwf解析器不支持dtype" [英] python read_fwf error: 'dtype is not supported with python-fwf parser'
问题描述
使用python 2.7.5和pandas 0.12.0,我试图使用'pd.io.parsers.read_fwf()'将固定宽度字体的文本文件导入到DataFrame中.我要导入的值都是数字,但是保留前导零非常重要,因此我想将dtype指定为字符串而不是int.
Using python 2.7.5 and pandas 0.12.0, I'm trying to import fixed-width-font text files into a DataFrame with 'pd.io.parsers.read_fwf()'. The values I'm importing are all numeric, but it's important that leading zeros be preserved, so I'd like to specify the dtype as string rather than int.
根据文档,该功能,read_fwf支持dtype属性,但是当我尝试使用它时:
According to the documentation for this function, the dtype attribute is supported in read_fwf, but when I try to use it:
data= pd.io.parsers.read_fwf(file, colspecs = ([79,81], [87,90]), header = None, dtype = {0: np.str, 1: np.str})
我得到了错误:
ValueError: dtype is not supported with python-fwf parser
我尝试过设置"dtype = something"的各种变体,但是它们都返回相同的消息.
I've tried as many variations as I can think of for setting 'dtype = something', but all of them return the same message.
任何帮助将不胜感激!
Any help would be much appreciated!
推荐答案
以@TomAugspurger的示例为基础,为要保留为str的列指定一个转换器,而不是指定dtypes:
Instead of specifying dtypes, specify a converter for the column you want to keep as str, building on @TomAugspurger's example:
from io import StringIO
import pandas as pd
data = StringIO(u"""
121301234
121300123
121300012
""")
pd.read_fwf(data, colspecs=[(0,3),(4,8)], converters = {1: str})
领先
\n Unnamed: 1
0 121 0123
1 121 0012
2 121 0001
转换器是从列名或索引到用于转换单元格中值的函数的映射(例如,int会将它们转换为整数,将float转换为float等)
Converters are a mapping from a column name or index to a function to convert the value in the cell (eg. int would convert them to integer, float to floats, etc)
这篇关于python read_fwf错误:"python-fwf解析器不支持dtype"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!