0

How can I find the index position of items in a list which satisfy a certain condition? Like suppose, I have a list like:

myList = [0, 100, 335, 240, 300, 450, 80, 500, 200] 

And the condition is to find out the position of all elements within myList which lie between 0 and 300 (both inclusive). I am expecting the output as:

output = [0, 1, 3, 4, 6, 8] 

How can I do this in pandas?

Also, how to find out the index of the maximum element in the subset of elements which satisfy the condition? Like, in the above case, out of the elements which satisfy the given condition 300 is the maximum and its index is 4. So, need to retrieve its index.

I have been trying many ways but not getting the desired result. Please help, I am new to the programming world.

1
  • 1
    How is myList in pandas? Is it a column, or it's supposed to be a list and not pandas? Commented Nov 13, 2019 at 18:19

3 Answers 3

1

You can try this,

>>> import pandas as pd >>> df = pd.DataFrame({'a': [0, 100, 335, 240, 300, 450, 80, 500, 200]}) >>> index = list(df[(df.a >= 0) & (df.a <= 300)].index) >>> df.loc[index,].idxmax() a 4 dtype: int64 

or using the list,

>>> l = [0, 100, 335, 240, 300, 450, 80, 500, 200] >>> index = [(i, v) for i, v in enumerate(l) if v >= 0 and v <= 300] >>> [t[0] for t in index] [0, 1, 3, 4, 6, 8] >>> sorted(index, key=lambda x: x[1])[-1][0] 4 

As Grzegorz Skibinski says, if we use numpy to get rid of many computations,

>>> import numpy as np >>> l = [0, 100, 335, 240, 300, 450, 80, 500, 200] >>> index = np.array([[i, v] for i, v in enumerate(l) if v >= 0 and v <= 300]) >>> index[:,0] array([0, 1, 3, 4, 6, 8]) >>> index[index.argmax(0)[1]][0] 4 
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, seriously rewriting it to numpy would save you a lot of computations. Rewriting your example: index = np.array([[i, v] for i, v in enumerate(l) if v >= 0 and v <= 300]) index[:,0] index[index.argmax(0)[1]][0]
Yeah, you're absolutely right! I added to my answer, thanks.
1

You can use numpy for that purpose:

import numpy as np myList =np.array( [0, 100, 335, 240, 300, 450, 80, 500, 200]) res=np.where((myList>=0)&(myList<=300))[0] print(res) ###and to get maximum: res2=res[myList[res].argmax()] print(res2) 

Output:

[0 1 3 4 6 8] 4 [Program finished] 

4 Comments

Hi..Thankyou for your help. But i need to retrieve the index of the maximum element and not the max element itself. Like in myList max element between 0 & 300 is 300 which lies at 4th position. So i need 4 as o/p and not 300. Can you please help me with this?
I updated my answer. You can look for your question.
E.Zeytinci Thank you for your update. It worked like a charm. How can I modify this code to find the index of the min element in the subset?
E.Zeytinci I got it :) ... just added reverse=True
0

This is between in pandas:

myList = [0, 100, 335, 240, 300, 450, 80, 500, 200] s= pd.Series(myList) s.index[s.between(0,300)] 

Output:

Int64Index([0, 1, 3, 4, 6, 8], dtype='int64') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.