一. 数据读取与预处理
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
%matplotlib inline
sns.set_context('poster', font_scale = 1.3)
df = pd.read_csv('redcard.csv.gz', compression = 'gzip')
print(df.shape)
df.head()
(146028, 28)
all_columns = df.columns.tolist()
# df.info()
# df.dtypes
df.describe().T
len(df.playerShort.unique())
len(df.player.unique())
2053
2034
print(df.height.mean())
print(np.mean(df.groupby('playerShort').height.mean()))
np.mean(df.groupby('player').height.mean())
181.93593798236887
181.74372848007872
181.7593471266707
二. 数据板块切分
2.1 创建子表并快速测试唯一性的辅助函数
player_index = 'playerShort'
player_cols = ['birthday', 'height', 'weight', 'position', 'photoID', 'rater1', 'rater2']
def get_subgroup(dataframe, g_index, g_columns):
g = dataframe.groupby(g_index).agg({
col:'nunique' for col in g_columns})
if g[g > 1].dropna().shape[0] != 0:
print("Warning: you probably assumed this had all unique values but it doesn't.")
return dataframe.groupby(g_index).agg({
col:'max' for col in g_columns})
players = get_subgroup(df, player_index, player_cols)
players.head()
2.2 Players子表
def save_subgroup(dataframe, g_index, subgroup_name, prefix = 'raw_'):
save_subgroup_filename = ''.join([prefix, subgroup_name, '.csv.gz'])
dataframe.to_csv(save_subgroup_filename, compression='gzip', encoding = 'UTF-8')
test_df = pd.read_csv(save_subgroup_filename, compression='gzip', index_col = g_index, encoding='UTF-8')
if dataframe.equals(test_df):
print('Test-passed: we recover the equivalent subgroup dataframe.')
else:
print('Warning -- equivalence test!!! Double-check.')
save_subgroup(players, player_index, 'players')
Test-passed: we recover the equivalent subgroup dataframe.
2.3 Clubs子表
club_index = 'club'
club_cols = ['leagueCountry']
clubs = get_subgroup(df, club_index, club_cols)
clubs.head()
clubs['leagueCountry'].value_counts()
save_subgroup(clubs, club_index, 'clubs')
2.4 Referees子表
referee_index = 'refNum'
referee_cols = ['refCountry']
referees = get_subgroup(df, referee_index, referee_cols)
referees.head()
print(referees.refCountry.nunique())
print(referees.shape)
save_subgroup(referees, referee_index, 'referees')
161
(3147, 1)
Test-passed: we recover the equivalent subgroup dataframe.
2.5 refCountry子表
country_index = 'refCountry'
country_cols = ['Alpha_3', 'meanIAT', 'nIAT', 'seIAT', 'meanExp', 'nExp', 'seExp']
countries = get_subgroup(df, country_index, country_cols)
countries = countries.rename(columns = {
'Alpha_3' : 'countryName'})
print(countries.shape)
countries.head()
(161, 7)
save_subgroup(countries, country_index, 'countries')
2.6 红牌Dyads子表
dyad_index = ['refNum', 'playerShort']
dyad_cols = ['games'