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
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.
CodePudding user response:
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)
)