我想要创建新的数据框列,循环遍历特定列的行。

3
我正在尝试使用Python创建Gale-Shapley算法,以实现医生和医院之间的稳定匹配。为此,我给每个医生和每个医院分配了一个随机偏好,用数字表示。
偏好数据框架

enter image description here

接下来,我创建了一个函数,为每个医生(由ID表示)评估每个医院,并创建两个新列来排名此评级。在评估匹配时,我取偏好之间的差异的绝对值,其中较小的绝对值表示更好的匹配。这是第一个医生的公式:

  doctors_sorted_by_preference['Rating of Hospital by Doctor 1']=abs(doctors_sorted_by_preference['Preference Doctor'].iloc[0]-doctors_sorted_by_preference['Preference Hospital'])
    doctors_sorted_by_preference['Rank of Hospital by Doctor 1']=doctors_sorted_by_preference["Rating of Hospital by Doctor 1"].rank()

以下是表格内容: 由医生偏好、评分和排名组成的数据框。

enter image description here

因此,医生1更喜欢第一个医院,因为它在排名中代表了所有其他医院。
现在,我想通过创建循环(为每个不同的医生创建两个新列并将它们添加到我的数据框中)来重复执行此函数,但我不知道该怎么做。我可以为所有10个不同的医生键入相同的函数,但如果我增加数据集以包括1000个医生和医院,这将变得不可能,必须有更好的方法...... 这将是医生2的相同功能:
doctors_sorted_by_preference['Rating of Hospital by Doctor 2']=abs(doctors_sorted_by_preference['Preference Doctor'].iloc[1]-doctors_sorted_by_preference['Preference Hospital'])
    doctors_sorted_by_preference['Rank of Hospital by Doctor 2']=doctors_sorted_by_preference["Rating of Hospital by Doctor 2"].rank()

提前感谢您!


3
最好将你的数据框作为文本插入,而不是图片,这样别人可以复制数据进行处理。print(doctors_sorted_by_preference)的结果应该足够了。 - asongtoruin
1个回答

2
你可以将值附加到列表中,然后将其写入数据框中。如果你有一个大型数据集,将值附加到列表中会更快。为了方便查看,我将数据框命名为df
最初的回答:您可以将值附加到列表中,然后将其写入数据帧中。如果您有大型数据集,则将其附加到列表中更快。 为了查看,我将我的数据框命名为df
for i in range(len(df['Preference Doctor'])):
    list1= []
    for j in df['Preference Hospital']:
         list1.append(abs(df['Preference Doctor'].iloc[i]-j))
    df['Rating of Hospital by Doctor_' +str(i+1)] = pd.DataFrame(list1)
    df['Rank of Hospital by Doctor_' +str(i+1)] = df['Rating of Hospital by Doctor_' 
                                                         +str(i+1)].rank()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接