jeudi 24 juin 2021

Get value From Previous or Next Rows Based on Condition from two or more columns in Python

I am using Pandas Python to calculate timedelta between rows which when it uses previous or next rows is based on conditions.

My table is like this sampel table

I want to create timedelta column with this condition: it gets value from previous rows when STATUS > status_before and gets value from next rows when STATUS < status_after.

I tried several ways to do this, and mostly ended up with this error:

The truth value of a Series is ambiguous. Use a.empty, a.bool(),a.item(), a.any() or a.all().

this one is one example of what I did:

if db.STATUS == 0 and db.status_before == 1:
   db.delta = db['REPORTDATE'].shift()

Any solutions?

Aucun commentaire:

Enregistrer un commentaire