Python 为什么我得到这个错误关键字:自治区

Python 为什么我得到这个错误关键字:自治区,python,pandas,Python,Pandas,我是Python的初学者。我合并了两个columnsAfter,尝试将一个列的“未赋值”值更改为另一个列值。我不能那样做。如果我使用预修改的数据帧,那么我可以更改 我从页面上抓取了一个表,然后修改了数据框中的数据 import pandas as pd import numpy as np import requests pip安装lxml 这是我使用的代码 我希望将邻域值更改为自治区值 我犯了这个错误 KeyError回溯(最近的呼叫 最后) /usr/local/lib/python3.6

我是Python的初学者。我合并了两个
columnsAfter
,尝试将一个列的“未赋值”值更改为另一个列值。我不能那样做。如果我使用
预修改的数据帧
,那么我可以更改

我从页面上抓取了一个表,然后修改了数据框中的数据

import pandas as pd
import numpy as np
import requests
pip安装lxml

这是我使用的代码

我希望将邻域值更改为自治区值

我犯了这个错误

KeyError回溯(最近的呼叫 最后) /usr/local/lib/python3.6/dist-packages/pandas/core/index/base.py in 获取位置(自身、键、方法、公差)2656尝试: ->2657返回自身引擎。获取定位(钥匙)2658,钥匙错误除外:

pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()

中的pandas/_libs/hashtable_class_helper.pxi pandas._libs.hashtable.PyObjectHashTable.get_item()

中的pandas/_libs/hashtable_class_helper.pxi pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError:“自治区”

在处理上述异常期间,发生了另一个异常:

KeyError回溯(最近的呼叫 最后)9帧 /usr/local/lib/python3.6/dist-packages/pandas/core/index/base.py in get_loc(自身、键、方法、公差)2657返回 自身发动机。获取位置(钥匙)2658,钥匙错误除外: ->2659返回self.\u引擎。获取loc(self.\u可能\u cast\u索引器(键))2660
索引器=self.get\u索引器([key],方法=method,容差=tolerance) 2661如果indexer.ndim>1或indexer.size>1:

pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()

中的pandas/_libs/hashtable_class_helper.pxi pandas._libs.hashtable.PyObjectHashTable.get_item()

中的pandas/_libs/hashtable_class_helper.pxi pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError:“自治区”


您的
keyerror
的原因是
neighbour
不是列,而是索引级别,解决方案是添加
reset\u索引

toronto_df1= pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')[0]

#boolean indexing        
toronto_df1 = toronto_df1.loc[toronto_df1['Borough']!="Not assigned"]
toronto_df2 = toronto_df1.groupby(['Postcode','Borough'],sort=False)['Neighbourhood'].agg(','.join).reset_index()
toronto_df2.loc[toronto_df2['Neighbourhood'] == "Not assigned", 'Neighbourhood'] = toronto_df2['Borough']
或参数
作为_index=False
groupby

toronto_df1= pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')[0]

#boolean indexing  
toronto_df1 = toronto_df1.loc[toronto_df1['Borough']!="Not assigned"]
toronto_df2=toronto_df1.groupby(['Postcode','Borough'],sort=False, as_index=False)['Neighbourhood'].agg(','.join)
toronto_df2.loc[toronto_df2['Neighbourhood'] == "Not assigned", 'Neighbourhood'] = toronto_df2['Borough']

检查“Borough”列是否与您所写的完全一样出现在数据集中。任何一端的额外空间都可能导致
键错误
非常感谢!我已经尝试过重置索引。但不知怎么的,它不起作用。但是as_index=False起作用了。再次感谢。
toronto_df1= pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')[0]

#boolean indexing  
toronto_df1 = toronto_df1.loc[toronto_df1['Borough']!="Not assigned"]
toronto_df2=toronto_df1.groupby(['Postcode','Borough'],sort=False, as_index=False)['Neighbourhood'].agg(','.join)
toronto_df2.loc[toronto_df2['Neighbourhood'] == "Not assigned", 'Neighbourhood'] = toronto_df2['Borough']