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?
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With