1

I have a big pandas dataframe from which I'm trying to select some rows with the .loc tool. The problem is that the condition I want to use in it needs an index which is given in one of the columns of the dataframe (the 'index' one). I try to select the row if the value is below a value that I need to found with the index in a simple list.

>>> df
r   v   index
1   2   2
2   4   3
3   20  1

>>> list
[3,6,32]

I want something like:

df.loc[ df['v'] < list[ df['index'] ] ]

So something which refers to the index in the studied row of the dataframe.

2

1 Answer 1

1

IIUC, convert the list to an array, and use "index" as the indexer:

v = np.array([3,6,32])
df[df['v'] < v[df['index'] - 1]]

   r  v  index
0  1  2      2
1  2  4      3

Where,

v[df['index'] - 1]
# array([ 6, 32,  3])

r = df.loc[df['v'] < v[df['index'] - 1]].copy()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.