【问题标题】:Pandas Raises ValueError When Trying To Use A List For Column NamesPandas 在尝试使用列表作为列名时引发 ValueError
【发布时间】:2016-09-18 05:42:10
【问题描述】:

下面的代码会导致 Pandas 引发 ValueError。我不确定为什么使用普通列表可以正常工作。

fileFields = [str(input("Please enter the column name for the pedigree field in
                  your request file.\n")),
              str(input("Please enter the column name for the pedigree field
                  in the Tissue Library file.\n")),
              str(input("Please enter the column name for the sourceID field
                  in the Tissue Library file.\n")),
              str(input("Please enter the column name for the pedigree field in 
                  the Gold Standard file.\n")),
              str(input("Please enter the column name for the sourceID field in
                  the Gold Standard file.\n"))]

dfRequests = pd.read_csv(fileInputs[0], skipinitialspace=True,
                         usecols=fileFields[0])
dfTissueLibrary = pd.read_csv(fileInputs[1], skipinitialspace=True,
                              usecols=fileFields[1:2])
dfGoldStandard = pd.read_csv(fileInputs[2], skipinitialspace=True,
                             usecols=fileFields[3:4])

结果:

Traceback (most recent call last):
  File "filepathway hidden for security", line 74, in <module>
    usecols=fileFields[0])
  File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 529, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 295, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 612, in __init__
    self._make_engine(self.engine)
  File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 747, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1154, in __init__
    col_indices.append(self.names.index(u))
ValueError: 'd' is not in list

我感觉好像 Pandas 正在从 fileFields 列表中的每个索引中获取字符串并将它们转换为字符串列表。我尝试通过在调用它们后制作索引字符串列表来解决这个问题,但这没有用。有什么建议吗?

【问题讨论】:

  • fieldField[0] 返回一个字符串(输入的第一列),所以d 可能是第一列的第一个字符,对吧?如果是,设置usecols=fieldFields

标签: python file pandas error-handling dataframe


【解决方案1】:

有什么建议吗?

我的方法是使用如下的小辅助函数,使过程简单且安全:

def selective_read_csv(purpose, path):
    # read just the header row and get the column names
    columns = list(pd.read_csv(path, nrows=1).columns.values)
    df = None
    while df is None:
        # present user with a selection of actual columns, taking
        # out the guess work
        file_fields = raw_input("[%s] Enter columns as a comma-separated list %s " % (purpose, columns))
        try:
            df = pd.read_csv(path, usecols=file_fields.split(','))
        except ValueError as e:
            print "Sorry, %s" % e
            df = None
    return df
df = selective_read_csv('requests file', '/tmp/data.csv')

通过这种方式,用户会收到文件中实际存在的列的提示,并且可以很好地处理错误输入:

[requests file] Enter columns as a comma-spearated list [u'a', u'b'] aaa
Sorry, 'aaa' is not in list
[requests file] Enter columns as a comma-spearated list [u'a', u'b'] 

然后为每个文件类型调用这个函数,例如:

dfRequests = selective_read_csv('requests file', fileInputs[0])
dfTissueLibrary = selective_read_csv('tissue library', fileInputs[1])
dfGoldStandard = selective_read_csv('gold standard', fileInputs[2])

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2022-01-12
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多