<jupyter_start><jupyter_text>RQ4<jupyter_code>import pandas as pd
# We import the data I need for RQ4

events_england = pd.read_json("events_England.json")
players = pd.read_json("players.json")
teams = pd.read_json("teams.json")
matches_england = pd.read_json("matches_England.json")
# We need to consider only the premier league's teams

for i in range( len( teams["area"] ) ):
    
    if (teams["area"][i]["name"] != "England" and teams["area"][i]["name"] != "Wales") or teams["type"][i] != "club":
        teams.drop([i], axis = 0, inplace=True)
        
teams
# We filter the events dataset to obtain only the "pass" events

pass_england = events_england[ events_england["eventName"] == "Pass" ]
pass_england.head()
# We only consider the columns of data that could be usefull; then we rename one of the 2 columns "wyId"
# (in this case the one in players_RQ4) to not confuse these between the 2 dataset

players_RQ4 = players[ ["currentTeamId", "firstName", "lastName", "wyId"] ]
teams_RQ4 = teams[ ["name", "wyId"] ]

players_RQ4 = players_RQ4.rename(columns={"wyId":"wyIdPlayer"})
players_RQ4.head()
teams_RQ4.head()
# We want associate each player to his team with pd.merge()

players_teams = pd.merge(players_RQ4, teams_RQ4, left_on="currentTeamId", right_on="wyId")
players_teams.head()
# We can forget about the "wyId" column because this info is already in the "currentTeamId" column

players_teams.drop("wyId", axis=1, inplace=True)
players_teams.rename( columns={"name":"TeamName"}, inplace=True)
players_teams.head()
# We define a new DataFrame, it contains all the playerID and the total number of pass that player did
# during the season

players_pass = pd.DataFrame( pass_england["playerId"].value_counts() )
players_pass.reset_index( level=0, inplace=True )
players_pass.rename( columns={ "playerId":"TotPass", "index":"playerId"}, inplace=True )
players_pass.head()
# Now we need to associate the "player_pass" DataFrame to the "players_team" DataFrame

players_pass = pd.merge( players_teams, players_pass, right_on="playerId", left_on="wyIdPlayer" )
players_pass.drop("wyIdPlayer", axis=1, inplace=True)
players_pass.head()
# We reset the index of pass_england DataFrame and then we pick only the successful passage, denoted by "id" 1801.
# Such "id" always occurs as the last element of the list pass_england["tags"][row], so it is possible to accede 
# to it by pass_england["tags"][i][-1] for every row i in the DataFrame

pass_england.reset_index(drop=True, inplace=True)
pass_success = pass_england[[pass_england["tags"][i][-1]["id"]==1801 for i in range(pass_england.shape[0])]]
pass_success.head()
# Now we can count, for each player, the number of successful passes and define a new DataFrame with 
# these informations

players_success_pass = pd.DataFrame( pass_success["playerId"].value_counts() )
players_success_pass.reset_index( level=0, inplace=True )
players_success_pass.rename( columns={ "playerId":"TotSuccPass", "index":"playerId2"}, inplace=True )
players_success_pass.head()
# Then we merge the latest informationn on the players_pass DataFrame

players_pass = pd.merge( players_pass, players_success_pass, left_on="playerId", right_on="playerId2" )
players_pass.drop("playerId2", axis=1, inplace=True)
players_pass.head()
# We can evaluate the rate of successful passages for each player dividing column TotSuccessPass with column TotPass.
# Then we sort the data frame according to this rate, in descending order 

players_pass = pd.concat([players_pass, players_pass.TotSuccPass/players_pass.TotPass], axis=1)
players_pass.rename( columns={0:"RatePass"}, inplace=True)
players_pass.sort_values(by=["RatePass"], ascending=False, inplace=True)
players_pass
# Filter for too low number of passes

players_pass = players_pass[ players_pass.TotPass > 100 ].sort_values(by=["RatePass"], ascending=False)
players_pass.head()
# A bit of Stat

mean_pass = players_pass.TotPass.mean()
std_pass = players_pass.TotPass.std()
# Filter with the Stat results

players_pass = players_pass[ players_pass.TotPass > mean_pass - std_pass ].sort_values(by=["RatePass"], 
                                                                                       ascending=False)
players_pass.reset_index( level=0, inplace=True )
players_pass.head()
# Finally, the players top 10 by successful passage rate

players_pass[:10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11834589901759196, 'fraction_numerical': 0.009824080420379255, 'mean_word_length': 5.593373493975903, 'pattern_counts': {'":': 7, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '408992', 'license': 'MIT', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1421, 'n_words': 407, 'path': 'codesforquestions/Question4.ipynb', 'repo_name': 'vedatk67/ADM-HW2', 'size': '4377'}