Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply two numerical columns on conditional using Pandas

I have pd dataframe (data) with three columns, X, Y and Z.

I need to run the following:

X * Y where Z = 'value'

I'm working along the lines of:

data[data['Z'] == 'value',[data['X']*data['Y']]]

Now I know that this isn't correct, but I can smell the correct answer. Can someone point me in the right direction?

like image 288
roastbeeef Avatar asked Dec 07 '22 16:12

roastbeeef


1 Answers

IIUC:

(df.X * df.Y).where(df.Z == 'Value')

or

df[df.Z == 'Value'].eval('X * Y')

Examples:

np.random.seed(123)
df = pd.DataFrame({'X':np.arange(10),'Y':np.arange(10),'Z':np.random.choice(['Value',np.nan],10)})

(df.X * df.Y).where(df.Z == 'Value')

0     0.0
1     NaN
2     4.0
3     9.0
4    16.0
5    25.0
6    36.0
7     NaN
8     NaN
9    81.0
dtype: float64

Or

df[df.Z == 'Value'].eval('X * Y')

0     0
2     4
3     9
4    16
5    25
6    36
9    81
dtype: int32
like image 142
Scott Boston Avatar answered Dec 22 '22 01:12

Scott Boston