ashnaa1610
ashnaa1610

Reputation: 31

Multiplying columns with missing values in Python (pandas)

I have a dataset with multiple columns which i need to multiply. One of these columns have missing values in them, what I would like is that when I am multiplying the columns, the missing values are skipped, and the columns which do have values in them are used for the result.

For example,

A B C

1 2 1

2 3 NaN

1 4 NaN

1 1 2

For each row, i would like the result to be column D with following values:-

2

6

4

2

I have tried .fillna(), .notnull(), .isnull() and .dropna() but I did not get the desired result.

Thanks in advance

Edit:

I had tried:

df['D'] = df['A'].fillna()*df['B']*df['C']

df

Upvotes: 0

Views: 1232

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Try:

df['C'].fillna(df['A'].mul(df['B']))

Upvotes: 0

webb
webb

Reputation: 585

Here's an example of utilizing .fillna():

import pandas as pd
import numpy as np

data = pd.DataFrame({"a":[3,6,7],"b":[2,5,7],"c":[5,np.nan,np.nan]})

A quick look at data:

a   b   c
3   2   5.0
6   5   NaN
7   7   NaN

Then utilize .fillna():

data.fillna(1).prod(axis=1)

Result:

30.0
30.0
49.0

I noticed that you used .fillna(). If you could include attempted code, it would help us debug your code for a more precise solution.

Upvotes: 2

Related Questions