ellen
ellen

Reputation: 704

How can I get specific row(s) from a pandas dataframe?

I'm trying to read through the pandas documentation but I can't seem to figure out what the best solution to my problem. I have a pandas dataframe called awards.

Each row of the dataframe represents an award, and each award has an ID. I want to pull an award out of the dataframe with a specific ID and use some of it's values for comparison. Please see the code snippet below.

I tried using this:

possible_awards = awards.loc[[awards['id'] ==  r_a['award_id']]]

but I don't think this is the best way. For one thing, I think (?) it should give me back an array or another dataframe, when really I know that there is only one award with that ID. Secondly, I don't know how to iterate through what this returns.

I want to be able to access the columns of the dataframe for this particular row, like here:

if possible_award['institution_id'] == award['institution_id'] and possible_award['department'] == award['department']:

However, I can't seem to do that when I iterate through whatever object 'possible_awards' is. I get the following error: 'string indices must be integers'

def foo(researcher, award, researchers_and_awards, awards):
    for r_a in researchers_and_awards:
       if r_a['researcher_id'] == researcher['id']:
            possible_awards = awards.loc[[awards['id'] ==  r_a['award_id']]] 
        for index, possible_award in possible_awards:
            if possible_award['institution_id'] == award['institution_id'] and possible_award['department'] == award['department']:
                    return True
            if possible_award['institution_id'] != award['institution_id'] and possible_award['competition_year'] != award['competition_year']:
                return True
return False

I want to find a clean and concise way to do this. Any help is appreciated! Let me know if I need to explain anything more.

Upvotes: 2

Views: 180

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

Ya can use:

possible_awards = awards[awards['id']==r_a['award_id']]

Then iterate like the below:

for idx,row in possible_awards.iterrows():
    # do whatever you want on this line with `row`

Upvotes: 1

Related Questions