2

I have been met with a problem. I have a list, called list A, and I have a data frame that has many columns. There are many time points in the first column. I want to select those rows that have the same values from list A. I tried to set the first column as an index. But somehow, it didn't work.

Any ideas as to what I'm doing wrong?

A=[8.1, 11.7, 13.475, 14.855, 15.125, 17.465, 19.82, 24.55]

First column is "Time". It's

[24.5,24.505,24.51,24.515,24.52,24.525,24.53,24.535,24.54,24.545,24.55,
24.555,24.56,24.565,24.57,24.575,24.58]
Sheldore
  • 37,862
  • 7
  • 57
  • 71
Ben Wang
  • 99
  • 3
  • 10

1 Answers1

-1

If you want the value,

for item in longList:
    if item in A:
        #The list item is found somewhere within the list A

Alternatively, if you want the index and/or the value,

for i in range(len(longList)):
    if longList[i] in A:
        #Index of item is i, value is longList[i]
Gigaflop
  • 390
  • 1
  • 13
  • Thanks. I also want to choose the rows based on the values we get from list A – Ben Wang Aug 02 '18 at 15:15
  • OP has a `pandas` `Dataframe`. Even if the OP didn't have that data structure your algorithm is inefficient. Better would be `set_A = set(A); found = [item for item in longlist if item in set_A`]. Look ups into a set are much more efficient than linear search into a list. But that's neither here nor there because `pandas` will end up with a very different looking solution. – Steven Rumbalski Aug 02 '18 at 15:19
  • @StevenRumbalski I filter out questions for `pandas`, this seems to have slipped through. Also, I didn't read the entire list of items (way too big) and assumed that there may have been duplicate values. – Gigaflop Aug 02 '18 at 15:40
  • They're time points so there is no duplicate value. I deleted some data. thanks – Ben Wang Aug 02 '18 at 15:51