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

Fill DataFrame based on value from another column

Given the following python pandas dataframe:

province district
Total example
NaN other
Other NaN
NaN example
Result example
NaN example

If the province column is NaN and the value for that row is ‘example’, I want to fill the province gap with ‘example’. The rest of the rows stay as they are.

DataFrame result:

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

province district
Total example
NaN other
Other NaN
example example
Result example
example example

>Solution :

You can use .fillna() conditionally with np.where:

df["province"] = np.where(
    df["district"] == "example", 
    df["province"].fillna(value="example"), 
    df["province"]
)
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