0
import pandas as pd

Data = {'participant': ['Jordan', 'Jess', 'Jake', 'Alice', 'Alan', 'Lauren'], 'Age': [26, 23, 19, 20, 24, 28], 'Sex': ['M', 'F', 'M', 'F', 'M', 'F'], 'BMI': [26, 22, 24, 17, 35, 20], 'Smokes': ['No', 'No', 'Yes', 'No', 'Yes', 'No']}
df = pd.DataFrame(Data)
print(df)
for name in 'participant':
  for ages in 'Age':
    for sexs in 'Sex':
      for Bmis in 'BMI':
        for smoke in 'Smokes':
          if nmb.find(str(30)) >= 30:
            print('participant')
  else: 
    print('none found')

question: What line of code would bring down the participants name that has a BMI of 30 or higher? How would i implant that?

1
  • Welcome on stack overflow. There is no need to iterate over the dataframe to filter it. You select rows from the dataset like selecting elements from a list. print( df[df["BMI"] >= 30] ) df["BMI"] will be the BMI column of the dataframe that will be compared element by element to 30. This boolean list is then used to select the respective rows. You should read more about python list basics and have a look into the pandas documentation to get started. Commented Apr 21, 2020 at 0:09

2 Answers 2

4

You should read on what the purpose of looping is, since this will definitely not do what you want. Also check Indexing and selecting data, which is what you should be doing here.

For what you want, you'd just need:

df.Age.ge(30).sum()
Sign up to request clarification or add additional context in comments.

1 Comment

is there the code that could come out with it saying who the person is that is above 30 BMI?
0

You asked for the participants names so simply do:

df[df['BMI']>=30][['participant']]

will do the trick. Result:

    participant   
4          Alan   

If you wish to have the result as an array: df[df['BMI']>=30][['participant']].values

As a side note: when you code and have nested for loops with many if statements - you're (very very likely) doing something wrong. Use it as a signal to come with a different (elegant) strategy.

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.