text_plain_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re
sns.set(color_codes=True, style='darkgrid')

games = pd.read_csv('../input/games.csv')

games = games[games.rated]
games['mean_rating'] = (games.white_rating + games.black_rating) / 2
games['rating_diff'] = abs(games.white_rating - games.black_rating)

games.victory_status.value_counts()

under_1500 = games[games.mean_rating < 1500]
under_2000 = games[games.mean_rating < 2000]
over_2000 = games[games.mean_rating > 2000]
brackets = [under_1500, under_2000, over_2000]
bracket_titles = ['Under 1500', 'Under 2000', 'Over 2000']

mate_games = games[games.victory_status == 'mate']
under_1500 = mate_games[mate_games.mean_rating < 1500]
under_2000 = mate_games[mate_games.mean_rating < 2000]
over_2000 = mate_games[mate_games.mean_rating > 2000]
m_brackets = [under_1500, under_2000, over_2000]

white_upsets = games[(games.winner == 'white') & (games.white_rating < games.black_rating)]
black_upsets = games[(games.winner == 'black') & (games.black_rating < games.white_rating)]
upsets = pd.concat([white_upsets, black_upsets])

THRESHOLD = 900
STEP = 50
u_percentages = []
print(f'Rating difference : Percentage of wins by weaker player')
for i in range(0 + STEP, THRESHOLD, STEP):
    th_upsets = upsets[upsets.rating_diff > i]
    th_games = games[games.rating_diff > i]
    upsets_percentage = th_upsets.shape[0] / th_games.shape[0] * 100
    u_percentages.append([i, upsets_percentage])
    print(f'{str(i).ljust(18)}:  {upsets_percentage:.2f}%')