Kaggleの流れ2(欠損値の多いデータの扱い)

データ(列)の欠損値が多いとseabornでヒストグラムを扱えない。

また、上位者は約20%の欠損値のある列は列ごと消している。

#データの読み込み
titanic=pd.read_csv("titanic_train.csv)
#欠損値をカウント、%を表示する関数
def null_count(df,v):
    total=df[v].isnull().sum()
    percent=total/len(df[v])*100
    return pd.DataFrame([[total,percent]],columns=["total","percent"],index=[v])

#Cabinのデータを確認
null_count(titanic,"Cabin")   

出力結果
    total    percent
Cabin    687  77.104377

pd.DataFrame([[ 中身、中身]],カラムの名前、、)

#Cabin列ごと消去
titanic=titanic.drop("Cabin",axis=1)

欠損値の多いままだと統計量出力の際に欠損値の除外されたデータが反映される+ヒストグラム作れない


この記事が気に入ったらサポートをしてみませんか?