Reputation: 11
Sorry for my english.
I have a question. I have this data frame and I would like to calculate the result of the profit that is in my dataframe : if Sellings - (Couts_fixe + Couts_Variables) >=0 : corporation tax = (Sellings - (Couts_fixe + Couts_Variables))*Taxes else: corporation tax = 0
I think it is something like that but that doesn't work.
I have Write this :
if (df['Sellings']-df['Couts_Tot']) >=0:
df['Taxes_Soc'] = (df['Sellings'] - df['Couts_Fixes'] - df['Couts_Variables'])*df['Taxes']
else : df['Taxes_Soc'] = 0
and they answers : "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
Thanks for your help.
Upvotes: 0
Views: 45
Reputation: 11
I have try this and it's work someone can say if its correct to write this like this in python or it's an unusual way to write it
df['Taxes_Soc'] = np.repeat(0, 12)
for i in range(12):
if (df['Sellings'][i]-df['Couts_Tot'][i]) >= 0:
df['Taxes_Soc'][i] = (df['Sellings'][i] - df['Couts_Fixes'][i] - df['Couts_Variables'][i])*df['Taxes'][i]
i += 1
else :
df['Taxes_Soc'][i] = 0
i += 1
Upvotes: 0
Reputation: 19250
You are getting the error The truth value of a Series is ambiguous
because you are using a pd.Series
of booleans in an if
statement. The if
statement expects a single boolean, so it doesn't know what to do with a Series of them.
What you are looking for is indexing with a boolean mask (a series of booleans). You can create your mask, and then modify rows depending on the value of the mask. The mask has a True or False value for each row.
The syntax ~mask
gets the boolean not
of the mask, so it switches the Trues to falses, and the falses to Trues.
mask = (df['Sellings'] - df['Couts_Tot']) >= 0
df.loc[mask, 'Taxes_Soc'] = (
(df.loc[mask, 'Sellings']
- df.loc[mask, 'Couts_Fixes']
- df.loc[mask, 'Couts_Variables'])
* df.loc[mask, 'Taxes'])
df.loc[~mask, 'Taxes_Soc'] = 0
Upvotes: 1