2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kaggle でよく使うコード

Last updated at Posted at 2019-06-30

CSVファイル処理

  • 読込み
train = pd.read_csv("../input/train.csv")
  • 書き出し(先頭列の列名を指定する必要がある。)
train.to_csv("my_train.csv", index_label = ["PassengerId"])

EDA(探索的データ解析)

  • 先頭5行表示
train.head()
  • 最大表示列数の指定(例として300列)
pd.set_option('display.max_columns', 300)
  • データの形状(行数、列数)を表示
train_shape = train.shape
  • 基本統計量(最大max、最小min、平均mean、標準偏差std)を表示。
train.describe()
  • 欠損数を確認。
train.isnull().sum()
  • 変数同士の相関を見る。(欠損値は除外される)
train.corr()

前処理

  • 欠損値を中央値で埋める。
train["Age"]=train["Age"].fillna(train["Age"].median())
  • ラベルを数値に変換。
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

le.fit(train["Sex"])
train["Sex"] = le.transform(train["Sex"])
  • 不要なカラムを削除。
train.drop("Fare",axis=1)
  • 列名を指定して取り出し。(2列を指定するDataFrame型で返ってくる)
a = train[["Age","Sex"]]
a = train.loc[:,["Age","Sex"]]
  • 列名を指定して取り出し。(1列を指定するとSeries型で返ってくる)
a = train["Age"]
a = train.loc[:,"Age"]
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?