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

Fillna only if the condition of another column is met

I have a dataframe test that looks like this:

| sales |transactions|
|-------|------------|
|0.0    |NaN         |
|0.0    |NaN         |
|3802.29|NaN         |
|4520.35|8359        |

I’m looking for a way to fill the NaN values with 0 of only the rows that have 0 in the ‘sales’ column, without changing the other rows. I tried this:

test['transactions'] = test.apply(
        lambda row: 0 if row['sales'] == 0 else None,
        axis=1)

It works for those rows but the problem is that fills with NaN all the other rows

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

Output:

| sales |transactions|
|-------|------------|
|0.0    |0.0         |
|0.0    |0.0         |
|3802.29|NaN         |
|4520.35|NaN         |

Expected result:

| sales |transactions|
|-------|------------|
|0.0    |0.0         |
|0.0    |0.0         |
|3802.29|NaN         |
|4520.35|8359        |

Thank you in advance.

>Solution :

mask

Specifically, use the other argument in mask

df.assign(
    transactions=df.transactions.mask(df.sales == 0, other=0)
)

     sales  transactions
0     0.00           0.0
1     0.00           0.0
2  3802.29           NaN
3  4520.35        8359.0

In the event you have a transaction that isn’t null where sales are zero and don’t want to replace a non-null transaction with zero then do:

mask = df.sales == 0 & df.transactions.isna()
df.assign(
    transactions=df.transactions.mask(mask, other=0)

)
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