数据来源:kaggle数据准备
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
inpath=r'C:\Users\......\Football\football-events\events.csv'
events=pd.read_csv(inpath) #导入主数据
events.isnull().sum() #缺失值汇总情况
path2=r'C:\Users\......\Football\football-events\dictionary.txt'
encoding=pd.read_table(path2,delim_whitespace=False,names=('num','events')) #导入解释性文件。因为主文件中都是用数字代替类型的
将主文件和解释性文件都导入python中
查看数据基本信息:
event.columns #主文件列名
event.isnull.sum()#汇总缺失值的信息
#提取各解释性文件,源文件为txt格式
event_type=encoding[1:13]
event_type_2=encoding[14:18]
side=encoding[19:21]
shot_place=encoding[22:35]
shot_outcome=encoding[36:40]
location=encoding[41:60]
bodypart=encoding[61:64]
assist_method=encoding[65:70]
situition=encoding[71:75]
将解释性文件保存,取消各解释性文件中的索引
event_type.to_csv(r'C:\Users\......\Football\football-events\event_type1.csv',index=False)
event_type_2.to_csv(r'C:\Users\......\Football\football-events\event_type_2.csv',index=False,header=False)
side.to_csv(r'C:\Users\......\Football\football-events\side.csv',index=False)
shot_place.to_csv(r'C:\Users\......\Football\football-events\shot_place.csv',index=False)
shot_outcome.to_csv(r'C:\Users\......\Football\football-events\shot_outcome.csv',index=False)
location.to_csv(r'C:\Users\......\Football\football-events\location.csv',index=False)
bodypart.to_csv(r'C:\Users\......\Football\football-events\bodypart.csv',index=False)
assist_method.to_csv(r'C:\Users\......\Football\football-events\assist_method.csv',index=False)
situition.to_csv(r'C:\Users\......\Football\football-events\situition.csv',index=False)
到目前为止,对文件的处理工作基本完成。当然,你也可以将解释性文件导入到主文件中,程序如下(各人觉得没必要):
y=[]
def tidai(x):
if x==1:
y='right foot'
if x==2:
y="left foot"
else:
y="head"
return y
goal1['events']=goal['bodypart'].apply(tidai) #将bodypart这个解释性文件内容导入到主文件中
对进球情况进行分析
# 选取有进