The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

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

--

Chapters
00:00 How To Use Complex Conditional To Fill Column By Cell In Pandas In A Efficient Way?
02:33 Accepted Answer Score 2
02:55 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