2022年6月17日金曜日

NBA選手データを整える

 NBAはデータを豊富に提供しているので分析に適しています。

ただし、身長や体重が日本と異なる表記なので、日本で分かりやすいようにデータを変換してみます。

ついでに欠損値除去や微調整してデータを取り扱い安くします。


まず、元データ player_data.csv の中身を確認します。

filename = "player_data.csv"

df = pd.read_csv(filename)

print(df.head())
print()
print(df.shape)
print()
print(df.info())


                  name  year_start  year_end position height  weight        birth_date                                college

0       Alaa Abdelnaby        1991      1995      F-C   6-10   240.0     June 24, 1968                        Duke University

1      Zaid Abdul-Aziz        1969      1978      C-F    6-9   235.0     April 7, 1946                  Iowa State University

2  Kareem Abdul-Jabbar        1970      1989        C    7-2   225.0    April 16, 1947  University of California, Los Angeles

3   Mahmoud Abdul-Rauf        1991      2001        G    6-1   162.0     March 9, 1969             Louisiana State University

4    Tariq Abdul-Wahad        1998      2003        F    6-6   223.0  November 3, 1974              San Jose State University


(4550, 8)


<class 'pandas.core.frame.DataFrame'>

RangeIndex: 4550 entries, 0 to 4549

Data columns (total 8 columns):

 #   Column      Non-Null Count  Dtype  

---  ------      --------------  -----  

 0   name        4550 non-null   object 

 1   year_start  4550 non-null   int64  

 2   year_end    4550 non-null   int64  

 3   position    4549 non-null   object 

 4   height      4549 non-null   object 

 5   weight      4544 non-null   float64

 6   birth_date  4519 non-null   object 

 7   college     4248 non-null   object 

dtypes: float64(1), int64(2), object(5)


データは4550件、birth_dateやweightに欠損値が散見されます。


print(df[df['weight'].isnull()])
print()
print(df[df['birth_date'].isnull()].sort_values('year_end', ascending=False))

df2 = df.dropna(how="any")
欠損値データを確認すると昔の人ばかりなので、昔の年代を分析しない場合は削除して構わないでしょう。dropna(how="any")で欠損値を取り除いたデータフレームにします。


# 1foot = 30.48, 1inch = 2.54cm
FOOT= 30.48
INCH = 2.54

# 1pond = 0.4536kg
LB = 0.4536

# フィートをセンチに
def toCenti(height):

    ft, inc = height.split("-")
    cent = int(ft) * FOOT + int(inc) * INCH

    return round(cent, 2)


# ポンドをキログラムに
def toKilogram(weight):

    kg = weight * LB
    
    return round(kg, 2)
    
 
# 以上の関数を身長体重データに適用させる
df2['height'] = df2['height'].apply(toCenti)
df2['weight'] = df2['weight'].apply(toKilogram)


print(df2.head())
print()
print(df2.shape)



                  name  year_start  year_end position  height  weight        birth_date                                college
0       Alaa Abdelnaby        1991      1995      F-C  208.28  108.86     June 24, 1968                        Duke University
1      Zaid Abdul-Aziz        1969      1978      C-F  205.74  106.60     April 7, 1946                  Iowa State University
2  Kareem Abdul-Jabbar        1970      1989        C  218.44  102.06    April 16, 1947  University of California, Los Angeles
3   Mahmoud Abdul-Rauf        1991      2001        G  185.42   73.48     March 9, 1969             Louisiana State University
4    Tariq Abdul-Wahad        1998      2003        F  198.12  101.15  November 3, 1974              San Jose State University

(4213, 8)

変換できました。

player_data_a.csv
完成データは 

0 件のコメント:

コメントを投稿