Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

students=df.loc[df[result]=="A"].head(5)
print(student["name"].to_list())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading