KeyError:'无法使用单个bool索引到setitem'

2024-10-06 06:58:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试设置数据框“df”中名为“barony2”的列的值,如果“add_urban”列包含ref_文件的当前iterrows“Townland”值,请使用以下代码。在

df = pd.read_csv('a.csv')
ref_file = pd.read_csv('b.csv')

for index, row in ref_file.iterrows():
    cond = str(row['Townland']) in df.add_suburb
    df.loc[cond, 'barony2'] = str(row['Barony'])

我得到KeyError: 'cannot use a single bool to index into setitem'任何帮助都非常感谢。在

^{pr2}$

Tags: csvinrefadddfreadindexfile