更改字典中数据帧外的值



我有一个类似的数据帧

name                         location   
0   Plaza Botero            ( -75.5686261812429,6.2524857541249315) 
1   Universidad de Medellín ( -75.6116092592174,6.231704691813073)
2   Parque Juanes de la Paz ( -75.56945687270888,6.290384323934336)
3   Parque del Poblado      ( -75.57088108434691,6.210362095508166) 
4   Alcaldía de Medellín    ( -75.57371337731854,6.2451496127526225)    
5   Parque Explora          ( -75.56556245591827,6.271208962002754) 

和一个像这样的路线字典:

routes= {0: [0, 1, 0], 1: [0, 2, 3, 0], 2: [0, 5, 4, 0]}

如何用坐标而不是索引创建一个新字典?

例如,对于路线0:

route_0= {0:[( -75.5686261812429,6.2524857541249315),( -75.6116092592174,6.231704691813073),( -75.5686261812429,6.2524857541249315)}

而不是列表中的0坐标(-75.55686261812429,6.2524857541249315(

非常感谢

尝试:

# convert `location` column to python tuples (skip this if they are tuples already):
from ast import literal_eval
df["location"] = df["location"].apply(literal_eval)
routes = {0: [0, 1, 0], 1: [0, 2, 3, 0], 2: [0, 5, 4, 0]}
x = pd.Series(routes).explode()
out = df.loc[x, "location"].groupby(x.index).agg(list).to_dict()
print(out)

打印:

{
0: [
(-75.5686261812429, 6.2524857541249315),
(-75.6116092592174, 6.231704691813073),
(-75.5686261812429, 6.2524857541249315),
],
1: [
(-75.5686261812429, 6.2524857541249315),
(-75.56945687270888, 6.290384323934336),
(-75.57088108434691, 6.210362095508166),
(-75.5686261812429, 6.2524857541249315),
],
2: [
(-75.5686261812429, 6.2524857541249315),
(-75.56556245591827, 6.271208962002754),
(-75.57371337731854, 6.2451496127526225),
(-75.5686261812429, 6.2524857541249315),
],
}

最新更新