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

STcoder

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?

Hanna

Try this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison

Deleting DataFrame row in Pandas based on column value

Fill missing value based on value from another column in the same row

Python pandas: Updating row based on value from another column

get a value of a specific row and column in pandas dataframe

Create a pandas column based on a lookup value from another dataframe

Replacing value in a column in pandas dataframe from a column value in another dataframe

Copy a row value to another column based on condition using Python pandas

How to update a column value in a row for specific value in another column in pandas dataframe?

Conditionally replace value in a row from another row value in the same column based on value in another column in Pyspark?

How to group rows based on specific value in a row and another column in pandas?

Replacing values in pandas dataframe column with same row value from another column

Pandas delete a column value if there exists a row based on another column value

Copy column value from one dataframe to another based on id in Pandas

pandas add column to dataframe having the value from another row based on condition

Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas

Calculate value using previously-calculated value (from the same column) and value from another column in a Pandas Dataframe

Selecting value of column based on the values in another column, then applying the value at each row in pandas dataframe

Extract Value From Pandas Dataframe Based On Condition in Another Column

How do I get the value of a column in a Pandas DataFrame with the name based on another columns value on the same row?

Pandas get column values of last N records as a list from another dataframe based on current row value

Pandas DataFrame: value in a column based on previous values in another column for same value in a third column

Pandas: check if string value in one column is part of string of another column in same row of dataframe - current script returning all Yes

Python Pandas: Append column value, based on another same column value

Impute Pandas dataframe column with value from another row based on ID column

Returning Column Index/Value Based on Max value of Specific row

for Loop for multiplying a value of each row with a specific column from another table based on condition in Python

Replace value in column in pandas dataframe based on another column value in same row?

Pandas replace the value of a column based on the value of the same column in another row