python2.7 运行下列 -- coding: utf-8 -- import pandas as pd import os df =…
- User
python2.7 运行下列
-- 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
运行这段代码的话,需要保证以下几点:
- 确保已经安装了pandas库。可以使用
pip install pandas命令进行安装。 - 确保安装了ExcelFile的依赖,可以使用
pip install xlrd命令进行安装。 - 将文件名为
1.xlsx的Excel文件放置在F:\GEE\wurenlongchang\目录下。
然后,你可以在Python 2.7的命令行或者运行Python脚本的环境下运行这段代码。运行后,会输出
种植作物列中第一个满足ID为8023的行的值。 - 确保已经安装了pandas库。可以使用
- User
-- coding: utf-8 --
import pandas as pd import os
df = pd.read_excel(r'F:\GEE\wurenlongchang\1.xlsx')
ID = df['ID' == 8023]
filter_df = df[ID]
crop = filter_df['种植作物'].iloc[0]
print (crop)
Traceback (most recent call last): File "f:/GEE/wurenlongchang/1.py", line 7, in ID = df['ID' == 8023] File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\frame.py", line 1997, in getitem return self._getitem_column(key) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column return self._get_item_cache(key) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache values = self._data.get(item) 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: False 如何解决 - Assistant
在你的代码中,有两个问题导致了
KeyError: False错误:- 在获取
ID那一行时,应该使用df[df['ID'] == 8023]而不是df['ID' == 8023]。 - 在获取
filtered_df之后,应该使用filtered_df作为筛选条件,而不是df[ID]。
以下是修改后的代码:
python# -- 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] crop = filtered_df['种植作物'].iloc[0] print(crop)请确保修改了这两个问题后再次运行代码。
- 在获取
- User
C:\Python27\ArcGIS10.7\lib\site-packages\pandas\indexes\base.py:1237: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return key in self._engine C:\Python27\ArcGIS10.7\lib\site-packages\pandas\indexes\base.py:1945: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return self._engine.get_loc(key) C:\Python27\ArcGIS10.7\lib\site-packages\pandas\indexes\base.py:1947: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return self._engine.get_loc(self._maybe_cast_indexer(key)) Traceback (most recent call last): File "f:/GEE/wurenlongchang/1.py", line 10, in crop = filter_df['绉嶆浣滅墿'].iloc[0] File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\frame.py", line 1997, in return self._getitem_column(key) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column return self._get_item_cache(key) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache values = self._data.get(item) 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: '\xe7\xa7\x8d\xe6\xa4\x8d\xe4\xbd\x9c\xe7\x89\xa9'如何解决 - Assistant
这个错误是由于
'种植作物'这个列名中包含了Unicode字符,而在你的代码中,是以Unicode字符串的形式进行访问的。为了解决这个问题,你可以尝试将filtered_df['种植作物'].iloc[0]中的Unicode字符串转换为普通字符串。以下是修改后的代码:
python# -- 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] crop = filtered_df[u'种植作物'].iloc[0] print(crop)通过在
filtered_df[u'种植作物']中使用u'种植作物'来表示Unicode字符串,可以避免UnicodeWarning错误。请修改代码并再次运行。 - User
Traceback (most recent call last): File "f:/GEE/wurenlongchang/excel.py", line 7, in df = pd.read_excel(r'F:\GEE\wurenlongchang\閬ユ劅鍦板潡淇℃伅.xlsx') File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\io\excel.py", line 170, in read_excel
io = ExcelFile(io, engine=engine) File "C:\Python27\ArcGIS10.7\lib\site-packages\pandas\io\excel.py", line 227, in init
self.book = xlrd.open_workbook(io) File "C:\Python27\ArcGIS10.7\lib\site-packages\xlrd_init_.py", line 395, in open_workbook
with open(filename, "rb") as f: IOError: [Errno 2] No such file or directory: 'F:\GEE\wurenlongchang\\\xe9\x81\xa5\xe6\x84\x9f\xe5\x9c\xb0\xe5\x9d\x97\xe4\xbf\xa1\xe6\x81\xaf.xlsx'-- coding: utf-8 --
import pandas as pd import os
df = pd.read_excel(r'F:\GEE\wurenlongchang\遥感地块信息.xlsx')
filtered_df = df[df['ID'] == 8023]
another_value = filtered_df[u'种植作物'].iloc[0]
print(another_value) 如何解决
- Assistant
这个错误是由于文件路径中包含了Unicode字符导致的。在你的代码中,文件路径是
'F:\\GEE\\wurenlongchang\\\\遥感地块信息.xlsx'。Python在Windows中使用的是ANSI编码,无法正确解析Unicode字符。要解决这个问题,可以尝试将文件路径转换为Unicode字符串。
以下是修改后的代码:
python# -- coding: utf-8 -- import pandas as pd import os df = pd.read_excel(ur'F:\GEE\wurenlongchang\遥感地块信息.xlsx') filtered_df = df[df['ID'] == 8023] another_value = filtered_df[u'种植作物'].iloc[0] print(another_value)通过在
pd.read_excel中使用ur'F:\GEE\wurenlongchang\遥感地块信息.xlsx'来表示Unicode字符串,可以解决文件路径中包含Unicode字符的问题。请修改代码并再次运行。