Reputation: 49
I have a dataframe with some data in it.
At the beginning, the data in the column "Current" changes from small values close to (-5, 0, +5) to values in a known range (let's say between -360 and -380). This goes on until I am not drawing current anymore. Then, the values are again close to 0.
I am basically trying to scan the dataframe and identify the first and last row where the values are between -360 and -380. Then, I want to use the row index and calculate the time interval between each of them.
My specific issue is to find the correct command and syntax to properly identify those two rows. Any clue ?
Here is a basic dataframe to illustrate. Basically, I'm looking for the index of rows 3 and 5. Once I have those rows, I can store this in a variable and then, calculate the equation (5 - 3). (I'm sorry, I don't really know how to format the result, but the code is below...)
df = pd.DataFrame([[1, 2], [2, 3], [3, 6],[4,370],[5,370],[6,370],[7,-5],[8,2]], columns=['A', 'B'])
Ind A B
0 1 2
1 2 3
2 3 6
3 4 370
4 5 370
5 6 370
6 7 -5
7 8 2
Many thanks for your help.
Upvotes: 0
Views: 484
Reputation: 51
Have you tried this:
col = 'current'
minVal = -380
maxVal = -360
begin = df.loc[np.logical_and(df[col]<maxVal, df[col]>minVal)].index[0]
end = df.loc[np.logical_and(df[col]<maxVal, df[col]>minVal)].index[-1]
What it basically does is select all the values in your given range and get the min index and max index. Note that noisy data points may cause problems.
Upvotes: 1