我有一个数据框 UFC_data ,其中包含与A,B和战斗机的UFC战斗的清单。
data = {'fighter_A':['Tony Ferguson', 'Al Iaquinta', 'Robbie Lawler'],
'fighter_B':['Donald Cerrone', 'Donald Cerrone', 'Donald Cerrone'],
'winner': ['Tony Ferguson', 'Donald Cerrone', 'Robbie Lawler']}
UFC_data = pd.DataFrame(data)
+-------+---------------+----------------+----------------+
| Index | fighter_A | fighter_B | winner |
+-------+---------------+----------------+----------------+
| 0 | Tony Ferguson | Donald Cerrone | Tony Ferguson |
| 1 | Al Iaquinta | Donald Cerrone | Donald Cerrone |
| 2 | Robbie Lawler | Donald Cerrone | Robbie Lawler |
+-------+---------------+----------------+----------------+
我创建了第二个数据框 fighter_list ,列出了我想完成的每架战斗机,并列出了每架战斗机的获胜次数
+-------+----------------+------+
| Index | fighter | wins |
+-------+----------------+------+
| 0 | Tony Ferguson | |
| 1 | Al Iaquinta | |
| 2 | Robbie Lawler | |
| 3 | Donald Cerrone | |
+-------+----------------+------+
直到现在我还无法返回每架战斗机的胜利次数
for index, row in fighter_list.iterrows():
fighter_list['wins'][index] = UFC_data['winner'].loc[UFC_data['winner'] == fighter_list['fighter'][index]].sum()
答案 0 :(得分:0)
使用Series.map
创建的Series
中的Series.value_counts
,最后用Series.fillna
替换丢失的值,并将值转换为整数:
s = UFC_data['winner'].value_counts()
fighter_list['wins'] = fighter_list['fighter'].map(s).fillna(0).astype(int)
print (fighter_list)
fighter wins
0 Tony Ferguson 1
1 Al Iaquinta 0
2 Robbie Lawler 1
3 Donald Cerrone 1
答案 1 :(得分:0)
定义以下功能:
def winCnt(row):
return UFC_data.winner[UFC_data.winner == row.fighter].count()
然后将其应用于每一行,并将结果保存在目标列中:
fighter_list['wins'] = fighter_list.apply(winCnt, axis=1)