-2

I am trying to save the 'yes' or 'no' results into a list that is named as outlier here.

This is my code

d = {'col1': [1, 2, 3, 4, 5], 'Spread': [10, 10.8, 5.0, 4.9,12.3]}
df = pd.DataFrame(d)
upper_limit = 9

rows = df.index.tolist()
outlier = []
for i in rows:
    if df.Spread[i]>upper_limit:
        result = print('yes') in outlier
    else:
        result = print('no') in outlier
         

and my output is like this

yes
yes
no
no
yes

after this loop, if I print outlier, it will only return to an empty list. What did I go wrong at this stage? How do I save 'yes' or 'no' results in the list?

Thanks in advance!

updated!

Susie
  • 93
  • 6
  • 1
    You need to return to your tutorial materials to see what the `print` command does. You seem to think that it returns a value useful to the program; this is at odds with the documented purpose of displaying values to the console. – Prune Apr 13 '21 at 23:43
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Your posted code fails due to an undefined data frame. – Prune Apr 13 '21 at 23:44
  • Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Apr 13 '21 at 23:44
  • @Prune sure just updated – Susie Apr 13 '21 at 23:51
  • I'll wait until the MRE is complete. – Prune Apr 13 '21 at 23:53
  • `print()` is used to display on screen - not to return value to variable. But often `displaying on screen` is called `returning data` which can be misleading. You need only `result = "yes"` and `result = "no"` - and you need smaller indentation in line `outlier.append(result)` – furas Apr 13 '21 at 23:58

1 Answers1

0

print() is used to display text on screen - not to return value to variable.
But often displaying on screen is called "returning data" which can be misleading.

You need only result = "yes" and result = "no".
And you need smaller indentation in line outlier.append(result)

outlier = []

for i in rows:

    # if/else
    if df.Spread[i] > upper_limit:
        result = 'yes'
    else:
        result = 'no'

    # after if/else
    outlier.append(result)   

or you can add directly

outlier = []

for i in rows:

    # if/else
    if df.Spread[i] > upper_limit:
        outlier.append('yes')   
    else:
        outlier.append('no')   

    # after if/else
    # ... nothing ...
    
furas
  • 134,197
  • 12
  • 106
  • 148