【问题标题】:Error when using panda module to read csv使用 panda 模块读取 csv 时出错
【发布时间】:2017-10-06 00:51:42
【问题描述】:
companyName = companyList[companyID-1]
data = pd.read_csv("C:\Feedback and Complaints_Sample Dataset.csv",sep=',')  # read file with panda module
desiredOutput = data["Co. Name" == companyName]
desiredOutput.to_csv("1.txt", sep = ",", index=False, header=False)
desiredOutput = data["Co. Name" == companyName]  # For desiredOutput, it'll be when co-name equals companyName

我在使用 panda 模块时收到一条错误消息。 请指教。 以下是错误信息:

   desiredOutput = data["Co. Name" == companyName]
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1797, in __getitem__
    return self._getitem_column(key)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "C:\Python27\lib\site-packages\pandas\core\index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3824)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
  File "pandas\hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12280)
  File "pandas\hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12231)
KeyError: False

【问题讨论】:

  • 如果您要问一些熊猫相关的问题,请添加pandas标签
  • 我被拒绝写熊猫标签,因为我没有 1500 声望
  • 从头开始,我可以写“熊猫”但不能写熊猫。我的错。
  • 你可以试试这个吗? desiredOutput = data.loc[data["Co. Name"].isin(companyName),:]
  • 这给出了这个错误:TypeError: only list-like objects are allowed to be pass to Series.isin(), you pass a 'str'

标签: python file pandas module keyerror


【解决方案1】:

如果您有 data 带有列的 DataFrame,其名称为 "Co. Name",那么您可以使用此代码的 sn-p 获取具有指定条件的所有行:

companyName = companyList[companyID-1]
data = pd.read_csv("C:\Feedback and Complaints_Sample Dataset.csv",sep=',')  # read file with pandas module
desiredOutput = data[data["Co. Name"] == companyName]

【讨论】:

    猜你喜欢
    • 2020-12-24
    • 2022-12-11
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2020-08-20
    相关资源
    最近更新 更多