Search code examples
pythonpandasdataframeif-statementvalueerror

Python, comparing dataframe rows, adding new column - Truth Value Error?


I am quite new to Python, and somewhat stuck here.

I just want to compare floats with a previous or forward row in a dataframe, and mark a new column accordingly. FWIW, I have 6000 rows to compare. I need to output a string or int as the result.

My Python code:

for row in df_an:

    x = df_an['mid_h'].shift(1)
    y = df_an['mid_h']

    if y > x:
        df_an['bar_type'] = 1

I get the error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The x and y variables are generated, but apparently things go wrong with the if y > x: statement.

Any advice much appreciated.

A different approach...

I managed to implement the suggested .gt operator.

df_an.loc[df_an.mid_h.gt(df_an.mid_h.shift()) &\
          df_an.mid_l.gt(df_an.mid_l.shift()), "bar_type"] = UP

Solution

  • Instead of row; you basically shifting whole row then comparing; try this once;

    df_an = pd.DataFrame({"mid_h":[1,3,5,7,7,5,3]}) 
    
    df_an['bar_type'] = df_an.mid_h.gt(df_an.mid_h.shift())
    
    # Output
    
       mid_h  bar_type
    0      1     False
    1      3      True
    2      5      True
    3      7      True
    4      7     False
    5      5     False
    6      3     False