text_html_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import trueskill as ts

def cleanResults(raceName,raceColumns,dfResultsTemp,dfResults):
    for raceCol in raceColumns:
        
        dfResultsTemp.index = dfResultsTemp.index.str.replace(r"(\w)([A-Z])", r"\1 \2")
        dfResultsTemp.index = dfResultsTemp.index.str.title()
        raceIndex = raceName + '-' + raceCol 
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].astype(str)
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].str.replace('\(|\)|DNF-|RET-|SCP-|RDG-|RCT-|DNS-[0-9]*|DNC-[0-9]*|OCS-[0-9]*','')
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].str.replace('DNF',str(len(dfResults)+1))
        dfResultsTemp[raceCol] = pd.to_numeric(dfResultsTemp[raceCol])  
        dfResultsTemp[raceIndex] = dfResultsTemp[raceCol]
        del(dfResultsTemp[raceCol])
        dfResults = pd.merge(dfResults,dfResultsTemp[[raceIndex]],left_index=True,right_index=True,how='outer')
        
    return dfResults

def doRating(numRaces, dfResults, dfRatings):
    for raceCol in range(1, numRaces + 1):
        competed = dfRatings['Name'].isin(dfResults['Name'][dfResults['R' + str(raceCol)].notnull()])
        rating_group = list(zip(dfRatings['Rating'][competed].T.values.tolist()))
        dfRatings['Rating'][competed] = ts.rate(rating_group, ranks=dfResults['R' + str(raceCol)][competed].T.values.tolist())
    return pd.DataFrame(dfRatings)

dfResults = pd.DataFrame()

dfResultsTemp = pd.read_csv('../input/20160323-LaVentana-HydrofoilProTour.csv')
dfResultsTemp = dfResultsTemp.set_index(dfResultsTemp['Name'] + ' ' + dfResultsTemp['LastName'])
raceColumns = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6']
dfResults = cleanResults('20160323-LaVentana-HydrofoilProTour', raceColumns, dfResultsTemp, dfResults)

dfResultsTemp = pd.read_csv('../input/20160807-SanFracisco-HydrofoilProTour.csv')
dfResultsTemp = dfResultsTemp.set_index(dfResultsTemp['Name'])
raceColumns = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12', 'R13', 'R14', 'R15', 'R16']
dfResults = cleanResults('20160807-SanFracisco-HydrofoilProTour', raceColumns, dfResultsTemp, dfResults)
dfResults