首页 > 解决方案 > 使用 iloc 创建新 col 和设置值时出错

问题描述

for i in range(0, len(df)):
    df.iloc[i,'county'] = get_county(df.iloc[i]['latitude'], df.iloc[i]['longitude'])

我正在尝试通过循环遍历数据框来创建一个名为county 的新列。这给了我一个错误IndexError: only integers, slices (::), ellipsis (...), numpy.newaxis () and integer or boolean arrays are valid indices

是否无法使用在新列中设置值df.iloc[i,'newcolname']

标签: pythonpandas

解决方案


推荐阅读