Returning a specific value from a column based on the value of another column in the same row in Python Pandas Dataframe

Question:

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?

Asked By: STcoder

||

Answers:

Try this:

students=df.loc[df[result]=="A"].head(5)
print(student["name"].to_list())
Answered By: Hanna
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.