Search code examples
python-3.xdataframepandas-datareader

Pandas not populating Column based on Condition


Experts,

I am having an issue with Pandas ..

Not sure why the below condition is not being satisfied.

df = pd.read_csv(path + filename, index_col='Date', parse_dates=True)
for i in range(1, len(signal)):

    if [(df['x'] < 2) & (df['y'] <= 0)]:
        listLongShort1.append("A_Condition")

# The other way around
    elif [(df['x'] > 3) & (df['y'] >= 1)]:
        listLongShort1.append("B_Condition")

    else:
        listLongShort1.append("No Condition")

It is just printing populating column with "A_Condition", for some reason not seeing elif or else.

Can you please advise what is wrong with my code ?

Thanks !!


Solution

  • & is a bitwise operator. AND is a operator you want to use to check conditions.

    if ((df['x'] < 2) and (df['y'] <= 0)):
        listLongShort1.append("A_Condition")
    elif ((df['x'] > 3) and (df['y'] >= 1)):
        listLongShort1.append("B_Condition")
    else:
        listLongShort1.append("No Condition")