0

I am trying to build logic that compares two columns in a dataframe after some logic has been verified.

Here is my code, it is pulling historical rates for cryptocurrency data from gdax.com. The test condition I am applying is 'if df.column 4 is greater than sum of df.column4 and df.column3 then buy 10% of account'

import GDAX
import pandas as pd
import numpy as np

public_client = GDAX.PublicClient()
ticker_call = public_client.getProductHistoricRates(product='LTC-USD')

df = pd.DataFrame(ticker_call)


df['account']=100



if df[4] > df[3] + df[4]:
    df['account_new'] = df['account']-df['account'] *.10
    print df

I am getting the error 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' for the if statement, what I dont understand is why? when I run each of the lines in the if statement individually they work. How can I fix this issue?

Why and how do I use a.bool() instead of an if statement?

thank you in advance.

1 Answer 1

1
df[4] > df[3] + df[4]

(that actually is equivalent to df[3] < 0) is a pandas.Series of boolean values. When do you want to enter the if statement? When all values are True? then you should use all(). When any of them is True? Then you should use any().

If instead you want to execute that function for any row when the condition is True, you should do something like

condition = df[4] > df[3] + df[4]
true_df = df[condition]
true_df["account_new"] = true_df['account']-true_df['account'] *.10

but now the columns "account_new" exists only in true_df, and not in df.

With something like

df["account_new"] = true_df["account_new"]

now also df has the column "account_new", but in the lines where condition is false you have nans...

Sign up to request clarification or add additional context in comments.

3 Comments

Hey @PietroTortella, that worked beautifully and you understood correct when all values are true is when I want to enter the if statement. Is it better to create conditional statements in pandas without the if statement and more so the way you made it?
It depends on the use you want to do... in your case doing if all(condition): is fine, in some other cases you'll need to perform an operation on the rows of a dataframe satisfying a condition, and you'll use the second strategy...
can chain multiple if-else statements under one all() clause? for example if all(condition): elseif(condtion):

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.