下午好,
我的代码有问题。我有一个类似于添加一个'Grid_frame3'的数据框,我想制作一个具有特定值的新数据框。在原始数据帧中,每一行都有x,y坐标和到达最后一列的时间。我想创建一个新的数据框,每个x,y坐标仅组合一次,并且到达最终值的时间将是这两个坐标可以找到的最小时间。当我运行我的代码时,它显示KeyError:1,请您帮我弄清楚为什么吗?
在错误消息中,它始终显示我开始将j用作迭代器的行。
我添加的数据帧仅与原始数据帧相似,因为原始数据帧非常大。
Grid_frame3 = {'flightID':['11111', '11111', '11111', '11111','2222','2222','2222','3333','3333','3333','3333'], 'timestamp':[1519669804, 1519669844, 1519669884, 1519669924,1519669976,1519679614,1519679615,1519679616,1519679800,1519679876,1519679999],'X':[1,1,1,1,2,3,4,4,4,5,6],'Y':[7,7,7,7,7,7,7,8,8,8,9],'time_to_final':[1,2,3,1,1,4,3,5,6,5,3]}
Grid_frame3 = pd.DataFrame(Grid_frame3)
column_names = ["X", "Y", "min_time"]
df = pd.DataFrame(columns = column_names)
for i in range(len(Grid_frame3)):
if len(df) == 0:
df.loc[i,"X"] = Grid_frame3.loc[i,"X"]
df.loc[i,"Y"] = Grid_frame3.loc[i,"Y"]
df.loc[i,"min_time"] = Grid_frame3.loc[i,"time_to_final"]
else:
for j in range(len(df)):
if Grid_frame3.X[i] == df.X[j] and Grid_frame3.Y[i] == df.Y[j] and Grid_frame3.time_to_final[i] > df.min_time[j]:
df.min_time[j] = Grid_frame3.time_to_final[i]
elif df.loc[j,"X"] == Grid_frame3.loc[i,"X"] and df.loc[j,"Y"] == Grid_frame3.loc[i,"Y"] and df.loc[j,"min_time"] <= Grid_frame3.loc[i,"time_to_final"]:
pass
else:
df.loc["X"] = Grid_frame3.loc[i,"X"]
df.loc["Y"] = Grid_frame3.loc[i,"Y"]
df.loc["min_time"] = Grid_frame3.loc[i,"time_to_final"]