如何从熊猫数据框中获取特定行?

时间:2018-12-30 02:12:02

标签: python pandas dataframe select

我正在尝试阅读熊猫文件,但似乎无法找出解决问题的最佳方法。我有一个称为奖赏的熊猫数据框。

数据框的每一行代表一个奖项,每个奖项都有一个ID。我想从数据框中提取具有特定ID的奖励,并使用其中的一些值进行比较。请参见下面的代码段。

我尝试使用这个:

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

但是我不认为这是最好的方法。一方面,我认为(?)当我真的知道只有那个ID的奖励时,它应该给我一个数组或另一个数据框。其次,我不知道如何遍历返回的结果。

我希望能够访问此特定行的数据框的列,例如:

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

但是,当我遍历对象“ possible_awards”时,似乎似乎无法做到这一点。我收到以下错误:“字符串索引必须是整数”

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

我想找到一种简洁明了的方法。任何帮助表示赞赏!让我知道是否需要进一步说明。

1 个答案:

答案 0 :(得分:1)

Ya可以使用:

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

然后像下面这样迭代:

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