
41
41
3.5 DataFrame のインデックス処理
3.5
DataFrame
のインデックス処理
「
1.4
DataFrame
」で
pandas
の
DataFrame
を読み込み、それを使ってデータカ
ラムの選択と変更を行いました。今度は、行選択を行いましょう。はじめに、乱数の
NumPy
配列を作り、それを使って
DataFrame
を初期化します。
>>> import numpy as np
>>> import pandas
>>> array = np.random .randn(4, 2)
>>> df = pandas.DataFrame(array)
>>> df
0 1
0 -0.143510 0.616050
1 -1.489647 0.300774
2 -0.074350 0.039621
3 -1.369968 0.545897
デフォルトでは、行もカラムもゼロから始まりますが、カラム名を指定することが
できます。
>>> col umns = ['A', 'B']
>>> df = pandas.DataFrame(array, columns=columns)
>>> df
A B
0 -0.143510 0.616050
1 -1.489647 0.300774
2 -0.074350 0.039621
3 -1.369968 0.545897
行名も与えられます。行名の集合をインデックス(
index
)と呼びます。名前その
ものはラベル(
label
)と呼びます。
>>> index = ['a', 'b', ...