0

I need to print from the series ('fluorescence') only the elements that are above a certain numerical threshold.

How can I take one element, compare it with another from the same series, and in case the condition is not met, simply move on in the series?

#for i in range(len(fluorescence)):
for i in fluorescence.iteritems():

   if i > 43: # (mean(fluorescence)-min(fluorescence)) == 43.
       print(i)

TypeError: '<' not supported between instances of 'tuple' and 'float'

This is the original dataframe: enter image description here

What I would like to have is the following:

enter image description here

Thank you in advance

  • Please update your question with the full error traceback. – quamrana Feb 06 '21 at 18:44
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. Off-site links and images of text are not acceptable, in keeping with the purpose of this site. – Prune Feb 06 '21 at 18:48
  • 1
    [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. Your posted code fails to run. – Prune Feb 06 '21 at 18:48
  • 1
    Please don't post images, post the textual data instead. – Sayandip Dutta Feb 06 '21 at 18:48

3 Answers3

0

filter() is what you need here.

# Filter will filter an iterator if the function returns True
fluorescence = [94, 35, 12, 104]
result = list(filter(lambda x: x > 43, fluorescence))
print(result)
# [94, 104]
Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
0

iteritems returns a tuple of (key, value). You can't compare the entire tuple to a scalar.

if i[1] > 43:

should fix your problem. See the other answer for a far better way to get your result.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

Pandas tolist() will convert the series to a list which is easily iterable.

#an empty list to store the values
new=[]
for i in fluorescence.tolist():
if i > 43
new.append(i)
print(new)
#print statement not in the loop
Natasha
  • 21
  • 3