给出示例数据集(实际数据具有(931, 674)
):
12_longitude_1 12_latitude_1 14_longitude_2 14_latitude_2 15_longitude_3 15_latitude_3
16 11 12 13 14 15
16 11 12 13 14 15
16 11 12 13 14 15
我正在跑步:
pd_out = pd.DataFrame({'zone': [], 'number': []})
for col_num in range(0, len(border.columns), 2):
curr_lon_name = border.columns[col_num]
curr_lat_name = border.columns[col_num + 1] # PROBLEM IS HERE
num = curr_lon_name.split("_")[-1]
border = border[[curr_lon_name, curr_lat_name]].dropna()
border[curr_lon_name] = border[curr_lon_name].replace(r'[()]', '', regex=True)
border[curr_lat_name] = border[curr_lat_name].replace(r'[()]', '', regex=True)
border[curr_lon_name] = pd.to_numeric(border[curr_lon_name], errors='coerce')
border[curr_lat_name] = pd.to_numeric(border[curr_lat_name], errors='coerce')
geometry2 = [Point(xy) for xy in zip(border[curr_lon_name],border[curr_lat_name])]
border_point = gpd.GeoDataFrame(border,crs=crs,geometry=geometry2)
turin_final = Polygon([[p.x, p.y] for p in border_point.geometry])
within_turin = turin_point[turin_point.geometry.within(turin_final)]
curr_len = len(within_turin)
pd_out = pd_out.append({'zone': "long_lat_{}".format(num), 'number': curr_len}, ignore_index=True)
但是在----> 7 curr_lat_name = border.columns[col_num + 1]
行中,我得到了
IndexError:索引3超出了大小为3的轴0的边界
答案 0 :(得分:0)
Python从0开始索引。因此,如果轴的大小为3,则只能使用索引0、1和2进行访问。
len(border.columns)
是3(我猜是3)。因此col_num
将在您的for循环中使用值0和2。
当它取值为2,然后在有问题的行中执行border.columns[col_num + 1]
时,您将尝试访问其边界之外的轴,因为col_num+1
为3。