0

Dataset

Error that I'm getting

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
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – mkrieger1 Apr 21 '20 at 18:06
  • So, the `diamond` that you're passing is a pandas dataframe? – Karl Knechtel Apr 21 '20 at 18:06
  • 1
    It would be nice if you could post the data as text. Data in images are hard to replicate and test. – Sajan Apr 21 '20 at 18:08
  • @mkrieger1 I don't think this question's a duplicate of the one you've marked. Please double-check and kindly retract your vote. – Mayank Porwal Apr 21 '20 at 18:27

1 Answers1

1

Something like this would work. I have created a sample df diamond with 3 rows like your dataframe:

One-liner:

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

More detailed way just for your understanding:

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
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58