Return all rows until row where criteria in specific column is met

Viewed 59

I have a DataFrame with a DateTime index and two columns with certain values.

I'm trying to check whether the values of one of the columns are greater or equal than the previous value of the same column (since I want to extract just a charging profile).

My attempt was:

df.diff() >= 0

but this returns ALL values that fulfill this condition (since it's a large dataset, values go up and down more than one time). What I want is to catch the values that fulfill the condition up to the first one that doesn't.

For example when considering column Y:

DataFrame:

                      X       Y
2021-03-26 15:36:40  0.59   8.19 
2021-03-26 15:36:50  0.59   8.41  
2021-03-26 15:37:00  0.59   8.18
2021-03-26 15:37:10  0.59   8.67
2021-03-26 15:37:20  0.59   8.71

Current output:

                      X       Y 
2021-03-26 15:36:50  0.59   8.41  
2021-03-26 15:37:10  0.59   8.67
2021-03-26 15:37:20  0.59   8.71

Desired output:

                      X       Y
2021-03-26 15:36:40  0.59   8.19 
2021-03-26 15:36:50  0.59   8.41  

Since I just want the values until the first "drop" of charge, regardless of what comes after it.

1 Answers

You can try to find the first index where the condition is true and then select every row untill that index:

df.loc[:df[df.Y.diff() >= 0].index[0]]

Returns:

                        X       Y
2021-03-26  15:36:40    0.59    8.19
            15:36:50    0.59    8.41
Related