在本文,我们将看到如何开始使用Python中的数据分析。我们在使用Python包:numpy,pandas,matplotlib和seaborn
使用的数据集是IPL(印度超级联赛)数据集,发布在来自cricsheet的Kaggle数据集中。
问题:
数据集中有多少匹配项?
数据集中有多少个季节?
哪支队伍赢得了最多的胜利?
哪支球队赢得了最高的门票?
哪支球队通过最接近的保证金赢得了(最少跑)?
哪支球队以最低门票获胜?
哪个赛季的比赛数量最多?
哪个IPL团队更成功?
Toss-winning有助于赢得比赛吗?
加载库
import numpy as np # numerical computing
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt #visualization
import seaborn as sns #modern visualization
设定图形
sns.set_style("darkgrid")
plt.rcParams['figure.figsize'] = (14, 8)
读取输入数据集
file_path = 'C:\Users\something\Downloads\'
matches = pd.read_csv(file_path+'matches.csv')
获取数据的基本信息
查看数据的行和列
matches.shape
(636, 18)
数据集的类型明细
matches.info()
RangeIndex: 636 entries, 0 to 635
Data columns (total 18 columns):
id 636 non-null int64
season 636 non-null int64
city 629 non-null object
date 636 non-null object
team1 636 non-null object
team2 636 non-null object
toss_winner 636 non-null object
toss_decision 636 non-null object
result 636 non-null object
dl_applied 636 non-null int64
winner 633 non-null object
win_by_runs 636 non-null int64
win_by_wickets 636 non-null int64
player_of_match 633 non-null object
venue 636 non-null object
umpire1 635 non-null object
umpire2 635 non-null object
umpire3 0 non-null float64
dtypes: float64(1), int64(5), object(12)
memory usage: 89.5+ KB
数据集内容摘要
matches.describe()
id
season
dl_applied
win_by_runs
win_by_wickets
umpire3
count
636.000000 636.000000 636.000000 636.000000 636.000000 0.0
mean
318.500000 2012.490566 0.025157 13.682390 3.372642 NaN
std
183.741666 2.773026 0.156726 23.908877 3.420338 NaN
min
1.000000 2008.000000 0.000000 0.000000 0.000000 NaN
25%
159.750000 2010.000000 0.000000 0.000000 0.000000 NaN
50%
318.500000 2012.000000 0.000000 0.000000 4.000000 NaN
75%
477.250000 2015.000000 0.000000 20.000000 7.000000 NaN
max
636.000000 2017.000000 1.000000 146.000000 10.000000 NaN
查看输入数据集的几个实际行
matches.head(2)
id
season
city
date
team1
team2
toss_winner
toss_decision
result
dl_applied
winner
win_by_runs
win_by_wickets
player_of_match
venue
umpire1
umpire2
umpire3
1 2017 Hyderabad 2017-04-05 Sunrisers Hyderabad Royal Challengers Bangalore Royal Challengers Bangalore field normal 0 Sunrisers Hyderabad 35 0 Yuvraj Singh Rajiv Gandhi International Stadium, Uppal AY Dandekar NJ Llong NaN
1
2 2017 Pune 2017-04-06 Mumbai Indians Rising Pune Supergiant Rising Pune Supergiant field normal 0 Rising Pune Supergiant 0 7 SPD Smith Maharashtra Cricket Association Stadium A Nand Kishore S Ravi NaN
数据集中有多少个匹配项?
正如我们已经看到的, id 是一个变量, 计算每个观察数据, 而每个观察是一个匹配。因此, 要获取数据集中的匹配项数, 可以获得数据集中的行数或变量 id 的最大值。
matches['id'].max()
636
数据集中有多少个季节?
像任何其他体育联盟一样,IPL每年发生一次,因此获得数据集中唯一年份的数量将告诉我们在数据集中有多少个季节。
matches['season'].unique()
array([2017, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016],dtype=int64)
列表的长度
len(matches['season'].unique())
10
哪支队伍赢得了最多的胜利?
matches.iloc[matches['win_by_runs'].idxmax()]
id 44
season 2017
city Delhi
date 2017-05-06
team1 Mumbai Indians
team2 Delhi Daredevils
toss_winner Delhi Daredevils
toss_decision field
result normal
dl_applied 0
winner Mumbai Indians
win_by_runs 146
win_by_wickets 0
player_of_match LMP Simmons
venue Feroz Shah Kotla
umpire1 Nitin Menon
umpire2 CK Nandan
umpire3 NaN
Name: 43, dtype: object
idxmax 将返回 maximumth 值的 id, 然后将其输入到 iloc 中以获取索引值并返回行。
如果我们只对该行中的获胜团队感兴趣, 则可以检索如下。
matches.iloc[matches['win_by_runs'].idxmax()]['winner']
'Mumbai Indians'
哪支球队赢得了最大的轮数?
matches.iloc[matches['win_by_wickets'].idxmax()]['winner']
'Kolkata Knight Riders'
哪支队伍赢得了(最接近的保证金)最低限额?
matches.iloc[matches[matches['win_by_runs'].ge(1)].win_by_runs.idxmin()]['winner']'Mumbai Indians'
我们已经将win_by_runs大于或等于1(通过消除零问题)的数据进行子集化。
哪支球队赢了最小轮数?
matches.iloc[matches[matches['win_by_wickets'].ge(1)].win_by_wickets.idxmin()]
id 560
season 2015
city Kolkata
date 2015-05-09
team1 Kings XI Punjab
team2 Kolkata Knight Riders
toss_winner Kings XI Punjab
toss_decision bat
result normal
dl_applied 0
winner Kolkata Knight Riders
win_by_runs 0
win_by_wickets 1
player_of_match AD Russell
venue Eden Gardens
umpire1 AK Chaudhary
umpire2 HDPK Dharmasena
umpire3 NaN
Name: 559, dtype: object
哪个赛季的比赛数量最多?
我们知道,如果每一行都是匹配的,那么计算每个季节的实例/行数会给我们每个季节的匹配数量。
sns.countplot(x ='season',data = matches)
plt.show()

最成功的 IPL 团队
#sns.countplot(y='winner', data = matches)
#plt.show
data = matches.winner.value_counts()
sns.barplot(y = data.index, x = data, orient='h');

比赛获胜者的最佳球员
top_players = matches.player_of_match.value_counts()[:10]
#sns.barplot(x="day", y="total_bill", data=tips)
fig, ax = plt.subplots()
ax.set_ylim([0,20])
ax.set_ylabel("Count")
ax.set_title("Top player of the match Winners")
#top_players.plot.bar()
sns.barplot(x = top_players.index, y = top_players, orient='v'); #palette="Blues");
plt.show()

Toss-winning有助于比赛获胜吗?
ss = matches['toss_winner'] == matches['winner']
ss.groupby(ss).size()
False 311
True 325
dtype: int64
要可视化结果:
#sns.countplot(matches ['toss_winner'] == matches ['winner'])
sns.countplot(ss);
