|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
提取两个字段出错,代码如下
- import pandas as pd
- # dataset = pd.read_excel("03.xlsx")
- data = pd.read_excel("03.xlsx",index_col=0)
- print(data.head(3))
- data = data["付款城市","中心"]
- print(data.head(3))
复制代码
下面是打印的内容
中心 客服
付款城市
鞍山 鞍山-库 李明
常州 常州-库 李明
大连 大连-库 李明
报错如下
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('付款城市', '中心')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythonProject/osfolderfile/pandas/pandas03xlsx.py", line 16, in <module>
data = data["付款城市","中心"]
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 3455, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: ('付款城市', '中心')
请问这个是啥情况
本帖最后由 isdkz 于 2022-2-22 15:53 编辑
列名不能是索引列,且提取多列要用列表,不能是元组
- import pandas as pd
- # dataset = pd.read_excel("03.xlsx") # 把 index_col=0 去掉
- data = pd.read_excel("03.xlsx")
- print(data.head(3))
- data = data[["付款城市","中心"]] # 加上 []
- print(data.head(3))
复制代码
|
|