The Python Oracle

How to use complex conditional to fill column by cell in pandas in a efficient way?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 Question
02:58 Accepted answer (Score 2)
03:31 Thank you

--

Full question
https://stackoverflow.com/questions/6064...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 2


If the available destino_heranca always appears first in each num_familia_raw, then you can do a transform:

test['destino_heranca'] = (test.groupby('num_familia_raw')['destino_heranca']
                               .transform('first')
                               .replace('','nao informado')
                           )

Output:

  destino_heranca  num_familia_raw
0               A                1
1               A                1
2   nao informado                2
3   nao informado                2
4               C                3
5               C                3