Focuspoints' Blog

A blog with My IT Life.


hey,欢迎来到我的小博客~

Pandas实战——NBA篮球队员成绩分析【修订版】

  • 在前一篇实战中,小编漏掉一个参数,以及没有按照实际情况考虑(同一个人多个赛季但还是这个人),导致结果有偏差。
  • 话不多说,直接上代码!
#@Time   : 2018/12/10 13:13
#@Author : focuspoints.cn

import pandas as pd
from numpy import *

csv_filename = 'player_regular_season.csv'
df = pd.read_csv(csv_filename,delimiter=",")
df_result = pd.DataFrame()


def load():
    global df_result
    global df
    for index, row in df.iterrows():
        if row['gp'] == 'N':
            df.drop(index, axis=0, inplace=True)
    df.fillna(0)
    df['gp'] = df['gp'].astype(int)
    # 合并数据
    df['name'] = df['firstname'] + ' ' + df['lastname']
    df = df.groupby(by='name').sum()
    df['Efficiency'] = ((df['pts'] + df['reb'] + df['asts'] + df['stl'] + df['blk']) - (
                (df['fga'] - df['fgm']) + (df['fta'] - df['ftm']) + df['turnover']))/df['gp']

def showSock(n,print_type):
    global df_result
    global df

    index_num = 0
    times = 27 - len("Name") - len(print_type) / 2
    print(5 * " ", "Name", " " * int(times), print_type)
    print(45 * "-")
    df_result = df.sort_values(print_type, ascending=False)
    for index,row in df_result.iterrows():
        if index_num == n :
            break
        else:
            result = row[print_type]
            length = len(index)
            times = 20 - length
            space = " " * times
            print(index, space, ":","%12d" % result)
        index_num += 1
    print(45 * "-")

def sreach():
    print("程序加载完成!")
    print("请输入要查询的值(英文缩写):")
    print("-" * 45)
    print("pts:得分 asts:助攻 stl:抢断 blk:盖帽")
    print("fga:投篮次数 fgm:命中次数 minutes:上场时间 reb:篮板")
    print("fta:罚球次数 ftm:罚球命中次数 turnover:失误 Efficiency:效率值")
    print("-" * 45)
    print_type = input()
    type_list = ["pts","asts","st1","blk","fga","fgm","minutes","reb","fta","ftm","turnover","Efficiency"]
    if print_type not in type_list:
        print("输入类型有误,请重新输入!")
        sreach()
    print("-" * 45)
    print("请输入查询数量:")
    print("-" * 45)
    n = int(input())
    print("-" * 45)
    showSock(n, print_type)

print("程序预加载中,请稍后···")
load()
sreach()
最近的文章

【软考中级】软件设计师笔记 - 数据库系统

数据库系统自用备份,Md编译器不支持部分功能。Typora编译器手搓数据库模式外模式→模式(概念模式…

软件设计师  软考中级  继续阅读
更早的文章

Pandas实战——NBA篮球队员成绩分析

引用资料下载:NBA球员数据.csv题目描述编写python程序,输入数目n,输出n个最有效率的球员…

pandas 数据分析 数据清洗 python  继续阅读
0评论