使用panda模块读取cs时出错

2024-04-19 22:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: inpandasdatagetindexliblinesite
1条回答
网友
1楼 · 发布于 2024-04-19 22:27:45

如果有dataDataFrame with column,其名称为"Co. Name",则可以使用以下代码片段获取具有指定条件的所有行:

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]

相关问题 更多 >