技术图文:基于“科比投篮”数据集学Pandas |
您所在的位置:网站首页 › 关于科比的话题 › 技术图文:基于“科比投篮”数据集学Pandas |
print(df.info) # # Int64Index: 30697 entries, 1 to 30697 # Data columns (total 24 columns): # # Column Non-Null Count Dtype # --- ------ -------------- ----- # 0 action_type 30697 non-null object # 1 combined_shot_type 30697 non-null object # 2 game_event_id 30697 non-null int64 # 3 game_id 30697 non-null int64 # 4 lat 30697 non-null float64 # 5 loc_x 30697 non-null int64 # 6 loc_y 30697 non-null int64 # 7 lon 30697 non-null float64 # 8 minutes_remaining 30697 non-null int64 # 9 period 30697 non-null int64 # 10 playoffs 30697 non-null int64 # 11 season 30697 non-null object # 12 seconds_remaining 30697 non-null int64 # 13 shot_distance 30697 non-null int64 # 14 shot_made_flag 25697 non-null float64 # 15 shot_type 30697 non-null object # 16 shot_zone_area 30697 non-null object # 17 shot_zone_basic 30697 non-null object # 18 shot_zone_range 30697 non-null object # 19 team_id 30697 non-null int64 # 20 team_name 30697 non-null object # 21 game_date 30697 non-null object # 22 matchup 30697 non-null object # 23 opponent 30697 non-null object # dtypes: float64(3), int64(10), object(11) # memory usage: 5.9+ MB 其各字段的含义如下所示: shot_id :投篮ID action_type :用什么方式投的篮 combined_shot_type :结合什么方式投篮 game_event_id :比赛时间id game_id :比赛ID lat :投篮的经度 loc_x :投篮的x坐标 loc_y :投篮的y坐标 lon :投篮的纬度 minutes_remaining :单场剩余时间(分钟) period :第几场 playoffs :是不是季后赛 season :赛季 seconds_remaining :剩余时间(秒) shot_distance :投篮离篮筐的的距离 shot_made_flag :是不是进球了(主要的标签) shot_type :2分球还是3分球区域 shot_zone_area :投篮区域的表示方法一 shot_zone_basic :投篮区域的表示方法二 shot_zone_range :投篮区域的表示方法三 team_id :队伍ID team_name :队伍名字 game_date :比赛时间 matchup :比赛双方队伍 opponent :对手 shot_id :投篮ID action_type :用什么方式投的篮 combined_shot_type :结合什么方式投篮 game_event_id :比赛时间id game_id :比赛ID lat :投篮的经度 loc_x :投篮的x坐标 loc_y :投篮的y坐标 lon :投篮的纬度 minutes_remaining :单场剩余时间(分钟) period :第几场 playoffs :是不是季后赛 season :赛季 seconds_remaining :剩余时间(秒) shot_distance :投篮离篮筐的的距离 shot_made_flag :是不是进球了(主要的标签) shot_type :2分球还是3分球区域 shot_zone_area :投篮区域的表示方法一 shot_zone_basic :投篮区域的表示方法二 shot_zone_range :投篮区域的表示方法三 team_id :队伍ID team_name :队伍名字 game_date :比赛时间 matchup :比赛双方队伍 opponent :对手 请解决如下问题 (a)哪种 action_type 和 combined_shot_type 的组合是最多的? (b)在所有被记录的 game_id 中,遭遇到最多的 opponent 是一个支?(由于一场比赛会有许多次投篮,但对阵的对手只有一个,本题相当于问科比和哪个队交锋次数最多) 参考答案: importpandas aspd df = pd.read_csv( 'data/Kobe_data.csv', index_col= 'shot_id') df_type = df.assign(type_1=df[ 'action_type'] + ' '+ df[ 'combined_shot_type']) print(df_type[ 'type_1'].value_counts.index[ 0]) # Jump Shot Jump Shot print(df_type[ 'type_1'].value_counts[ 0]) # 18880 df_game = df[[ 'game_id', 'opponent']] print(df_game.drop_duplicates[ 'opponent'].value_counts.index[ 0]) # SAS print(df_game.drop_duplicates[ 'opponent'].value_counts[ 0]) # 91 从上面程序的运行结果可以看出,科比投篮最常用的方式就是跳投 Jump Shot ,在科比的职业生涯中与 圣安东尼奥马刺队(SAS)队交锋最多,达到91次。 知识点 1. 列的添加 classDataFrame(NDFrame): defassign(self, **kwargs)-> "DataFrame": 使用 assign 方法可以在原有数据集中添加列,在本案例中就是添加了 type_1 列,该列为 action_type 和 combined_shot_type 的组合,以方便确定该组合中哪一种出现的次数最多。 2. count和value_counts count 函数:返回非缺失值元素个数。 value_counts 函数:返回每个元素有多少个。 count 函数:返回非缺失值元素个数。 value_counts 函数:返回每个元素有多少个。 defvalue_counts( self, normalize=False, sort=True, ascending=False, bins=None, dropna=True ) : classSeries(base.IndexOpsMixin, generic.NDFrame): defcount(self, level=None): 使用 value_counts 可以得到该列中每个元素出现的次数,并由大到小排列。 3. 重复元素处理 drop_duplicates 函数:返回删除了重复行的Series或DataFrame。 drop_duplicates 函数:返回删除了重复行的Series或DataFrame。 defdrop_duplicates(self, keep= "first", inplace=False) -> Optional["Series"]: classDataFrame(NDFrame): defdrop_duplicates( self, subset: Optional[Union[Hashable, Sequence[Hashable]]] = None, keep: Union[str, bool] = "first", inplace: bool = False, ignore_index: bool = False, ) -> Optional["DataFrame"]: 由 game_id 和 opponent 构造新的数据集,并对该数据集 drop_duplicates 去重,就能得到每场比赛的对手球队。根据 value_counts 可以得到与每个对手球队比赛的次数并由大到小排列。 4. 第一章 Pandas基础 的知识导图 总结 通过做练习题来巩固 Pandas 的知识点是熟悉 Pandas 的一种有效方式,以往遇到问题直接就摸索着来用了,这次跟着 joyful-pandas系统的学习一遍夯实基础。大家一起努力。See You! 后台回复「搜搜搜」,随机获取电子资源! 返回搜狐,查看更多 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |