Skip to content

You're working as a sports journalist at a major online sports media company, specializing in soccer analysis and reporting. You've been watching both men's and women's international soccer matches for a number of years, and your gut instinct tells you that more goals are scored in women's international football matches than men's. This would make an interesting investigative article that your subscribers are bound to love, but you'll need to perform a valid statistical hypothesis test to be sure!

While scoping this project, you acknowledge that the sport has changed a lot over the years, and performances likely vary a lot depending on the tournament, so you decide to limit the data used in the analysis to only official FIFA World Cup matches (not including qualifiers) since 2002-01-01.

You create two datasets containing the results of every official men's and women's international football match since the 19th century, which you scraped from a reliable online source. This data is stored in two CSV files: women_results.csv and men_results.csv.

The question you are trying to determine the answer to is:

Are more goals scored in women's international soccer matches than men's?

You assume a 10% significance level, and use the following null and alternative hypotheses:

: The mean number of goals scored in women's international soccer matches is the same as men's.

: The mean number of goals scored in women's international soccer matches is greater than men's.

# Start your code here!
import pandas as pd
#open the csv files to familiarize with the datasets
women_results= pd.read_csv("women_results.csv")
women_results.columns
print(women_results.head)
men_results= pd.read_csv("men_results.csv")
print(men_results.head(5))
men_results.columns
#get only te rows for fifa world cup tournament
#filter for years from 2002-01-01
women_fifa= women_results.loc[(women_results["tournament"]== 'FIFA World Cup') & (women_results["date"] >= "2002-01-01")]
print(women_fifa)

#men
men_fifa= men_results.loc[(men_results["tournament"]== 'FIFA World Cup') & (men_results["date"]>= "2002-01-01")]
print(men_fifa)
#sum of scores in the two datasets

men_fifa["group"] = "men"
women_fifa["group"] = "women"


women_fifa["sum"]= women_fifa["away_score"] + women_fifa["home_score"]
print(women_fifa.head())

women_data= women_fifa[["tournament", "sum", "group"]]
women_data
#men_fifa
men_fifa["sum"]= men_fifa["away_score"] + men_fifa["home_score"]
print(men_fifa.head())

men_data= men_fifa[["tournament", "sum", "group"]]
men_data
#concatenate the dfs
combined_df= pd.concat([women_data, men_data], axis=0)
combined_df
#pivot the dataframe
combined_pivot= combined_df.pivot(columns= "group", values= "sum")
combined_pivot
#Perform the MWU test
from scipy.stats import mannwhitneyu
import pingouin

mwu_test= pingouin.mwu(x=combined_pivot["women"],y=combined_pivot["men"], alternative= "greater")
mwu_test
#get the p_val
mwu= mwu_test["p-val"]
p_val= mwu[0]
#result of the hypothesis
alpha= 0.1

if p_val != alpha:
    result= "reject"
else:
    result="fail to reject"
    
print(result)
#print results
result_dict = {"p_val": p_val, "result": result}
print(result_dict)