Skip to content
Advertisement

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:

JavaScript

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.

JavaScript

Advertisement

Answer

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

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement