Skip to content
Advertisement

Iterate over column values matched value based on another column pandas dataframe

This is a followup to

extract column value based on another column pandas dataframe

I have more than one row that matches the column value and want to know how to iterate to efficiently retrieve each value when there are multiple matches.

Dataframe is

JavaScript

The below will always pick p3

JavaScript

So I tried to iterate like

JavaScript

And it prints

JavaScript

for all matching values

However is there a more efficient way to do this?

Advertisement

Answer

You can get all matching values by without using .iloc[0] in the df.loc code, as follows:

JavaScript

Output:

JavaScript

The 2 4 on the left of the output are the original row indices. You can use this information if want to know from which rows are these 2 extracted data originated from.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement