Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How fill NA with respect of another column?

I want to fill NaN values in this way: put the same values as the column B if they have the same value in B

Example:

A    B
nan 'ra'
9   'ra'
5   'pa'

So NaN value in column A should be 9 because they have the same values in column B.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

df['A'] = df.groupby('B')['A'].ffill().bfill()

Output:

>>> df
     A   B
0  9.0  ra
1  9.0  ra
2  5.0  pa
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading