Home > Software design >  Returning a specific value from a column based on the value of another column in the same row in Pyt
Returning a specific value from a column based on the value of another column in the same row in Pyt

Time:10-20

I am quite new to Python dataframes.

In the following case:

id name result
1 Cara A
2 Ben B
3 Evie A
4 Mel C

I want to return the name of the student who has the first occurrence of result A (Cara). I want to then go and do the same for the second occurrence of A (Evie) and repeat until 5 occurrences. How would I do this?

CodePudding user response:

Try this:

students=df.loc[df[result]=="A"].head(5)
print(student["name"].to_list())
  • Related