我是Python的初学者。我合并了两个columnsAfter
,我试图将一列的“未分配”值更改为另一个列值。我不能那样做。如果我使用premodified dataframe
,那么我可以进行更改。
我从页面上抓取了一个表格,然后修改了该数据框中的数据。
import pandas as pd
import numpy as np
import requests
pip install lxml
toronto_url='https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M'
toronto_df1= pd.read_html('https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M')[0]
toronto_df1.head()
toronto_df1.drop(toronto_df1.loc[toronto_df1['Borough']=="Not assigned"].index, inplace=True)
toronto_df2=toronto_df1.groupby(['Postcode','Borough'],sort=False).agg(lambda x: ','.join(x))
toronto_df2.loc[toronto_df2['Neighbourhood'] == "Not assigned", 'Neighbourhood'] = toronto_df2['Borough']
这是我使用的代码。
我希望将区值更改为自治市值。
我收到此错误。
KeyError跟踪(最近的呼叫 持续) /usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py在 get_loc(自身,键,方法,公差)2656尝试: -> 2657返回self._engine.get_loc(key)2658,除了KeyError:
pandas._libs.index.IndexEngine.get_loc()中的pandas / _libs / index.pyx
pandas._libs.index.IndexEngine.get_loc()中的pandas / _libs / index.pyx
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/indexes/base.py在 get_loc(自身,键,方法,公差)2657返回 self._engine.get_loc(key)2658,除了KeyError: -> 2659返回self._engine.get_loc(self._maybe_cast_indexer(key))2660
pandas._libs.index.IndexEngine.get_loc()中的
索引器= self.get_indexer([键],方法=方法,公差=公差) 2661如果indexer.ndim> 1或indexer.size> 1:pandas / _libs / index.pyx
pandas._libs.index.IndexEngine.get_loc()中的pandas / _libs / index.pyx
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:“镇定”
答案 0 :(得分:0)
您的keyerror
的原因是Neighbourhood
不是列,而是索引级别,解决方案是添加reset_index
:
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']
或将参数as_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']