kaggleのTitanicのデータを利用して、データフレーム型(DataFrame)に対する基本操作を行っていきます。
データフレーム型はpandasの主要なデータ構造の一つで、行とカラムで構成された可変のテーブルで構成されています。
Titanicのデータは下記のページから取得できます(アカウント登録が必要です)。今回はtrain.csvを利用します。
Titanic: Machine Learning from Disaster
スポンサーリンク
開発環境
Python 3.5.1 :: Anaconda 2.5.0 (x86_64)
jupiter 4.0.6
pandas 0.18.0
1 2 3 4 5 6 7 8 9 |
import pandas as pd # ファイルを読み込む df = pd.read_csv('train.csv') print(type(df)) #始めから3行分のみ表示 df[0:3] |
結果
<class ‘pandas.core.frame.DataFrame’>
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th… | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
train.csvは既にデータフレーム型であることが分かります。もし配列をデータフレーム型に転換する場合は、df = pd.DataFrame(array) のように記述します。
1 2 3 4 5 6 7 8 |
#行数 len(df)#891 #(行数,列数) df.shape#(891, 12) #カラム情報 print(df.info()) |
結果
<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null object
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB
None
1 2 3 4 |
#特定のカラムを抽出 df_name = df[['Name']] df_name[0:3] |
結果
Name | |
---|---|
0 | Braund, Mr. Owen Harris |
1 | Cumings, Mrs. John Bradley (Florence Briggs Th… |
2 | Heikkinen, Miss. Laina |
1 2 3 4 |
#複数のカラムを抽出 df_name_sex = df[['Name','Sex']] df_name_sex[0:3] |
結果
Name | Sex | |
---|---|---|
0 | Braund, Mr. Owen Harris | male |
1 | Cumings, Mrs. John Bradley (Florence Briggs Th… | female |
2 | Heikkinen, Miss. Laina | female |
df.iloc[0:3,3:5] や df.ix[0:2,3:5] で同じ結果を出力することができます。
1 2 3 4 5 6 |
#カラム名を指定して抽出 male = df[df.Sex == "male"] print(len(male)) female = df[df.Sex == "female"] print(len(female)) |
結果
577
314
1 2 |
#年齢の平均 df.Age.mean() |
結果
29.69911764705882
1 2 3 |
#カラムを付け足す df['New']= '1' df[0:3] |
結果
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | New | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 1 |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th… | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 1 |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 1 |
Newという名のカラムが追加され、全ての要素に1が入力されているのが分かります。カラムを削除する場合は、delを利用します。
1 2 |
#Newカラムを削除する del df['New'] |
1 2 3 |
#Genderカラムを追加し、Sexカラムの要素であるmale/femaleを1/0に変換。要素として入力していく。 df['Gender'] = df['Sex'].map( {'male': 1, 'female': 0} ).astype(int) df[0:3] |
結果
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | Gender | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 1 |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th… | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 0 |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 0 |
1 2 |
#Pclass(乗船クラス階級)でグループ分けし年齢の平均を出力 df.groupby('Pclass').Age.mean() |
結果
Pclass
1 38.233441
2 29.877630
3 25.140620
Name: Age, dtype: float64
1 2 3 |
#カテゴリーデータ(質的データ)を、ダミー変数に変換する。 sex = pd.get_dummies(df['Sex']) sex[0:3] |
結果
female | male | |
---|---|---|
0 | 0.0 | 1.0 |
1 | 1.0 | 0.0 |
2 | 1.0 | 0.0 |
1 2 3 |
#データフレームを結合する df = pd.concat([df,sex],axis=1) df[0:3] |
結果
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | Gender | female | male | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 1 | 0.0 | 1.0 |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th… | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 0 | 1.0 | 0.0 |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 0 | 1.0 | 0.0 |
ダミー変数に変換したSexデータフレームを、元からあるdfデータフレームと結合しています。axis=1の1は列を意味します(行は0)。
参照ページ
pandas 0.18.0 documentation