如何通过查找表进行绘图(字典)

时间:2019-07-14 06:39:26

标签: python-3.x dictionary matplotlib seaborn

我想根据字典来绘制值。

因此,如果我有此DataFrame:

match_winners = pd.DataFrame({'match_id':[1,2,3,4,5,6],
                              'player_1':['Sarah', 'Gorge', 'Mike', 'Steve', 'Madlin', 'Ali'],
                              'player_2':['Mike', 'Ali', 'Madlin', 'Sarah', 'Gorge', 'Steve'],
                              'winner':['Sarah', 'Ali', 'Mike', 'Sarah', 'Gorge', 'Steve']})

我也有这本字典:

myDict = {'team_1': ['Sarah', 'Madlin', 'Gorge'],
          'team_2': ['Ali', 'Mike', 'Steve']}

我希望seaborn.countplot()绘制出team_1,team_2的数量,而不是直接绘制获胜者列。

这是直接图:

sb.countplot(data = match_winners, x = 'winner');

The plot link

1 个答案:

答案 0 :(得分:0)

有没有比这更好的方法了?

for idx, row in match_winners.iterrows():
    if  match_winners.loc[idx,'winner'] in myDict['team_1']:
        match_winners.loc[idx,'team_winner'] = 'team_1'
    elif match_winners.loc[idx,'winner'] in myDict['team_2']:
        match_winners.loc[idx,'team_winner'] = 'team_2'