0

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()."

as you can see we have an monthly selling and cost and taxes

Thanks for your help.

0

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This should work, but it is discouraged to iterate over the rows of a pandas dataframe. It is preferable to use vectorized operations, meaning operations that apply to the dataframe at once (without a python for loop).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.