Search code examples
pandasgeometrypointgeopandas

Pandas new column with same value for certain rows


I have a dataframe df that looks like this:

Sezione         Indirizzo                   geometry
6              Via Carlo e Valeria Jülg, 9  None
6              Via Carlo e Valeria Jülg, 9  None
...            ...                          ...
3              Via di Revolta, 4            None
3              Via di Revolta, 4           None

and a dictionary addresses like this:

{
 3: 0    POINT (11.10417 46.01877) Name: geometry, dtype: geometry,
 
 6: 0    POINT (11.12534 46.07222) Name: geometry, dtype: geometry
 

I would like to assign to the geometry column the corresponding values for all the rows that have the same Sezione:

Sezione         Indirizzo                   geometry
6              Via Carlo e Valeria Jülg, 9  POINT (11.12534 46.07222)
6              Via Carlo e Valeria Jülg, 9  POINT (11.12534 46.07222)
...            ...                          ...
3              Via di Revolta, 4            POINT (11.10417 46.01877)
3              Via di Revolta, 4            POINT (11.10417 46.01877)

I tried the following but applies the value to the first row and doesn't continue.

for i in range(1, len(addresses)):
    df.loc[df.Sezione == i, "geometry"] = addresses[i]

trying assigning directly obviously fails for warnings. Any ideas of how to do this?


Solution

  • Resolved with a nice lambda

    df["geometry"] = df["Sezione"].apply(lambda x: addresses[x])