roastbeeef
roastbeeef

Reputation: 1119

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?

Upvotes: 4

Views: 5358

Answers (4)

Luis O López
Luis O López

Reputation: 31

i think you want somenthing like this:

import pandas as pd
import numpy as np

df_original = pd.DataFrame({'X': [1, 2, 3, 4, 5, 6],
                            'Y': [7, 8, 9, 10, 11, 12],
                            'Z': [False, True, True, True, False, False]})

df_original['X*Y'] = np.where(df_original.Z == True, df_original.X * df_original.Y, df_original.Z)
#In this case True or False are the conditios or "Value", but you can put any value you want.

Output:

   X   Y      Z  X*Y
0  1   7  False    0
1  2   8   True   16
2  3   9   True   27
3  4  10   True   40
4  5  11  False    0
5  6  12  False    0

Upvotes: 1

piRSquared
piRSquared

Reputation: 294328

Setup
Borrowed from @ScottBoston

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

Solution

df.loc[df.Z.eq('Value'), ['X', 'Y']].prod(1) 

0     0
2     4
3     9
4    16
5    25
6    36
9    81
dtype: int64

Upvotes: 3

Arthur Gouveia
Arthur Gouveia

Reputation: 744

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

Here's a working example:

dataframe = pd.DataFrame({'X': [1, 2, 3, 4, 5, 6],
                          'Y': [5, 6, 7, 8, 9, 0],
                          'Z': [0, 1, 0, 1, 0, 1]})

dataframe.loc[dataframe['Z'] == 0, 'Z'] = dataframe.loc[dataframe['Z'] == 0, 'X'] * dataframe.loc[dataframe['Z'] == 0, 'Y']

print(dataframe)

#    X  Y   Z
# 0  1  5   5
# 1  2  6   1
# 2  3  7  21
# 3  4  8   1
# 4  5  9  45
# 5  6  0   1

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

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

Upvotes: 9

Related Questions