I asked the question in the following post and it got closed:
and it got closed by @Brad Solomon and marked as duplicate. But the suggested links did not helped in my solution. So again my questions:
I am trying to calculate a value and making a if statement based on Product, but I could not find a good and easy why how it should work. I am just receiving an error but no proper solution. Maybe someone knows how to fix it?
The code looks like the following:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date' : ['11/8/2011', '11/8/2011', '11/8/2011'],
'ID' : ['ID 1', 'ID 2', 'ID 3'],
'A' : [100, 200, 50],
'B' : [0.2, 0.5, 0.9],
'C' : [0.02, 0.05, 0.09],
'Product' : ['P1', 'P2', 'P1'],
'Type' : ['Food', 'Non-Food', 'Food']
})
def calc_1(df):
calc = np.log(df['A'] / df['B']) + (df['C'])
return calc
def calc_2(df):
calc2 = 0.0
d1_calc = 0.0
d1_calc = calc_1(df)
if df['Product'] == 'P1':
if df['Type'] == 'Food':
calc2 = d1_calc*np.log(df['A']/(df['B']*df['C']))
elif df['Product'] == 'P2':
if df['Type'] == 'Non-Food':
calc2 = d1_calc*np.exp(df['A']/(df['B']*df['C']))
else:
raise Exception('No Calc')
return calc2
calculation = calc_2(df)
print(calculation)
I am getting the following error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-d5a5d719b0f8> in <module>
----> 1 calculation = calc_2(df)
2 print(calculation)
<ipython-input-12-4304ba3859f1> in calc_2(df)
5 d1_calc = calc_1(df)
6
----> 7 if df['Product'] == 'P1':
8 if df['Type'] == 'Food':
9 calc2 = d1_calc*np.log(df['A'])
~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Aucun commentaire:
Enregistrer un commentaire