Home > Mobile >  Returning the location of variables in a range of a pandas dataframe column where a condition is met
Returning the location of variables in a range of a pandas dataframe column where a condition is met

Time:06-10

I currently have a column in a dataframe, df[Stress]. I want to return the location of the rows in the column where the value stored is less than a variable, load_drop, but only within a certain range of the column, stated by first and last. I figured I could use np.where to find the locations, but so far I'm returning an empty array when I run the code. Here is what I have so far:

df = {'Stress': [1,2,3,6,7,8,10,12,14,20,19,17,15,13,12,10,8,7,6,4,1,0]

first = 10
last = 18
drop = 11

life_array = np.where(df['Stress'].iloc[first:last] < drop)

print (life_array)

[]

Ideally, my desired output would be this:

print(life_array)

0  15
1  16
2  17
3  18

Which is the the location of the rows where the condition is met. Can I use np.where and iloc in such a fashion? Any help would be greatly appreciated!

CodePudding user response:

IIUC need 2 steps - first filter by position and then by mask:

s =  df['Stress'].iloc[first:last]
life_array = s[s < drop]

print (life_array)
15    10
16     8
17     7
Name: Stress, dtype: int64

If need indices:

first = 10
last = 18
drop = 11
s =  df['Stress'].iloc[first:last   1]
life_array = s.index[s < drop].tolist()

print (life_array)
[15, 16, 17, 18]
  • Related