有两个字典:
Latitud = {'Cundinamarca': 4.6 , 'Norte de Santander': 7.9, 'Antioquia': 6.2}
Longitud = {'Cundinamarca': -74.1 , 'Norte de Santander': -72.5, 'Antioquia': -75.5}
并且我已经尝试在Dataframe中创建2个新列,并考虑了'Departamento'列,并使用以下代码:
df['Latitud'] = df['Departamento'].map(Latitud)
df['Longitud'] = df['Departamento'].map(Longitud)
但是我有这个:
Departamento , Municipio , Latitud , Longitud
Cundinamarca , Bogota , Nan , Nan
Cundinamarca , Bogota , Nan , Nan
Norte de Santander , Cúcuta , Nan , Nan
Antioquia , Medellín , Nan , Nan
如何解决?谢谢
答案 0 :(得分:1)
可能是不区分大小写的问题,我们可以尝试使用小写字母映射:
df['Latitud'] = df['Departamento'].str.lower().map({k.lower() : v
for k, v in Latitud.items()})
与Longitud
可能有必要替换空白:
df['Latitud'] = (df['Departamento'].str.lower()
.str.replace(' ', '').map({k.lower() : v
for k, v in Latitud.items()})
)