Reputation: 33
Below are the fields of my data frame: serial, depth, x, y, z
I'm trying to find the volume (xyz) if the depth field is greater than 55, else set a default value of 6.
But I'm getting the error "Truth value of the series is ambiguous". Would someone please guide me as to where my mistake is?
def my_if(diamond):
if diamond.depth > 55:
diamond['New']= diamond.x*diamond.y*diamond.z
else:
diamond['New'] = 6
Upvotes: 0
Views: 55
Reputation: 34066
Something like this would work. I have created a sample df diamond
with 3 rows like your dataframe:
In [2041]: diamond['volume'] = np.where(diamond.depth > 55, diamond.x * diamond.y * diamond.z, 6)
In [2042]: diamond
Out[2042]:
depth x y z volume
0 61.5 3.95 3.98 2.43 38.202030
1 52.8 3.89 3.84 2.31 6.000000
2 55.2 4.05 4.07 2.31 38.076885
Step-1: Divide your dataframe into 2 parts. First, with depth > 55
:
In [2004]: df1 = diamond[diamond.depth > 55]
Step-2: For the above obtained df1, multiply x,y and z to get volume:
In [2016]: df1['volume'] = df1.x * df1.y * df1.z
Step-3: Create another dataframe(df2
) with depth <= 55
:
In [2020]: df2 = diamond[diamond.depth <= 55]
Step-4: Hardcode volume to 6:
In [2021]: df2['volume'] = 6
Concat both dataframes(df1
and df2
) to get the complete result:
In [2024]: pd.concat([df1,df2])
Out[2024]:
depth x y z volume
0 61.5 3.95 3.98 2.43 38.202030
1 59.8 3.89 3.84 2.31 34.505856
2 54.2 4.05 4.07 2.31 6.000000
Upvotes: 1