2

I have a dataframe with millions of lines. I need to get the corresponding indexes of hundreds of thousands of elements that are present in the original dataframe.

I currently use this code:

df[df['processed_col'] == element.index[0]

to find the position of 'element' into the whole dataframe.

Instead of doing a loop which is very long, is there a way to take a list like element1, element2,..., elementN as input, which would return a list of corresponding indices:

df[df['processed_col'] == [element1, element2, ..., elementN].index[0]
4
  • Hmmm, you need df[df['processed_col'].isin([element1, element2,..., elementN])].index[0] Commented Aug 24, 2016 at 10:25
  • Sorry your question is unclear are you asking for df[df['processed_col'].isin(some_list)].index? Commented Aug 24, 2016 at 10:26
  • @jezrael I think the OP wants the index values where the values exist, index[0] will return the first index value Commented Aug 24, 2016 at 10:27
  • Yes, if need only first value, use index[0], if need all values, use index... Commented Aug 24, 2016 at 10:28

1 Answer 1

3

IIUC you need isin if find first value of index:

df[df['processed_col'].isin([element1, element2,..., elementN])].index[0]

or if want all values of index, remove [0] only:

df[df['processed_col'].isin([element1, element2,..., elementN])].index

If need convert to list use tolist:

df[df['processed_col'].isin([element1, element2,..., elementN])].index.tolist()
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.