File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\internals.py", line 3290, in…
- User
File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\internals.py", line 3290, in get
loc = self.items.get_loc(item) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368) File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322) KeyError: '\xe5\x9c\xb0\xe5\x9d\x97\xe5\x90\x8d\xe7\xa7\xb0/ID'-- coding: utf-8 --
import pandas as pd import os
df = pd.read_excel(r'F:\GEE\wurenlongchang\1.xlsx')
filtered_df = df[df['地块名称/ID'] == 8023]
another_value = filtered_df['种植作物'].iloc[0]
print(another_value)如何结局
- Assistant
It seems that the KeyError is occurring at this line in your code:
pythonfiltered_df = df[df['地块名称/ID'] == 8023]The error message suggests that there is no column named '地块名称/ID' in your DataFrame. To resolve this issue, you can try the following steps:
- Double-check the column names in your DataFrame. Make sure that the column name '地块名称/ID' is spelled correctly and matches the column name in the Excel file you are reading.
- Verify that the column name is correctly encoded. It appears to be in UTF-8 encoding, but double-check to ensure it is correct.
- If the column name is correct, try removing any leading or trailing spaces from the column name. You can use the
str.strip()method to remove leading and trailing spaces from the column names in your DataFrame. - If the issue persists, check if the column name is present in the Excel file itself. Open the file and verify that the column name exists in the header row.
If none of these solutions work, please provide more information about your data and the file you are reading from, so we can further assist you.