jeudi 24 septembre 2020

Replace only specific values in df column based on specific value in another column

I have the following datframe:

>>> name   ID     geom                                                geometry_error
0  Lily   1234  POLYGON ((5.351418786 7.471461148, 5.352018786...     overlap
1  Pil    3248  POLYGON ((7.351657486 9.341445548, 1.346718786...     overlap
2  Poli   9734  -                                                     -
0  Lily   1234  POLYGON ((5.351265486 2.471876538, 6.33355018786...   overlap

I want to "edit" the geometry_erro column, with a condition that if geom value is '-' , the geometry error value will be "no geometry", e.g:

>>> name   ID     geom                                                geometry_error
0  Lily   1234  POLYGON ((5.351418786 7.471461148, 5.352018786...     overlap
1  Pil    3248  POLYGON ((7.351657486 9.341445548, 1.346718786...     overlap
2  Poli   9734  -                                                     no geometry
0  Lily   1234  POLYGON ((5.351265486 2.471876538, 6.33355018786...   overlap

I have tried to do it with this:

def gg(row):
    if row['geom'] == '-':
        val = 'no geometry generated'   
    return val

df['geometry errors'] = df.apply(gg, axis=1)

>>>UnboundLocalError: local variable 'val' referenced before assignment

I don't understand why I get this error because I have used this varuabke name val in different function in the same script so why now do I get this? and is there maybe better way to do it?

Aucun commentaire:

Enregistrer un commentaire