7.1.2 データの選択
分析用のデータの準備の一般的なステップは、大規模なデータセットから対象の値を選択またはフィルタ処理することです。
この項の各例では、oml.DataFrame
オブジェクトから行、列および値によってデータを選択する方法を示します。
これらの例では、次のコードによって作成されたoml_iris
オブジェクトを使用します。このコードは、sklearn.datasets
パッケージをインポートし、iris
データセットをロードします。これは、x
およびy
変数を作成した後、永続データベース表IRISと、その表のプロキシとしてoml.DataFrame
オブジェクトoml.iris
を作成します。
import oml
import pandas as pd
from sklearn import datasets
# Load the iris data set and create a pandas.DataFrame for it.
iris = datasets.load_iris()
x = pd.DataFrame(iris.data, columns = ['Sepal_Length','Sepal_Width',
'Petal_Length','Petal_Width'])
y = pd.DataFrame(list(map(lambda x: {0: 'setosa', 1: 'versicolor',
2:'virginica'}[x], iris.target)),
columns = ['Species'])
# Create the IRIS database table and the proxy object for the table.
oml_iris = oml.create(pd.concat([x, y], axis=1), table = 'IRIS')
次の各トピックで例を示します。
最初または最後の行数の選択
head
およびtail
メソッドは、最初または最後の要素数を返します。
選択されるデフォルトの行数は5です。
例7-1 最初および最後の行数の選択
この例では、oml.DataFrame
オブジェクトoml_iris
から行を選択します。oml_iris
の最初の5行と10行、次に最後の5行と10行を表示します。
# Display the first 5 rows.
oml_iris.head()
# Display the first 10 rows.
oml_iris.head(10)
# Display the last 5 rows.
oml_iris.tail()
# Display the last 10 rows.
oml_iris.tail(10)
この例のリスト
>>> # Display the first 5 rows.
... oml_iris.head()
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
>>>
>>> # Display the first 10 rows.
... oml_iris.head(10)
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
5 5.4 3.9 1.7 0.4 setosa
6 4.6 3.4 1.4 0.3 setosa
7 5.0 3.4 1.5 0.2 setosa
8 4.4 2.9 1.4 0.2 setosa
9 4.9 3.1 1.5 0.1 setosa
>>>
>>> # Display the last 5 rows.
... oml_iris.tail()
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 6.7 3.0 5.2 2.3 virginica
1 6.3 2.5 5.0 1.9 virginica
2 6.5 3.0 5.2 2.0 virginica
3 6.2 3.4 5.4 2.3 virginica
4 5.9 3.0 5.1 1.8 virginica
>>>
>>> # Display the last 10 rows.
... oml_iris.tail(10)
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 6.7 3.1 5.6 2.4 virginica
1 6.9 3.1 5.1 2.3 virginica
2 5.8 2.7 5.1 1.9 virginica
3 6.8 3.2 5.9 2.3 virginica
4 6.7 3.3 5.7 2.5 virginica
5 6.7 3.0 5.2 2.3 virginica
6 6.3 2.5 5.0 1.9 virginica
7 6.5 3.0 5.2 2.0 virginica
8 6.2 3.4 5.4 2.3 virginica
9 5.9 3.0 5.1 1.8 virginica
列によるデータの選択
例7-2 列によるデータの選択
この例では、oml_iris
から2列を選択し、それらを使用してoml.DataFrame
オブジェクトiris_projected1
を作成します。その後、iris_projected1
の最初の3行を表示します。この例ではさらに、oml_iris
から一連の列を選択し、iris_projected2
を作成して、その最初の3行を表示します。最後に、データ型によってoml_iris
から列を選択し、iris_projected3
を作成して、その最初の3行を表示します。
# Select all rows with the specified column names.
iris_projected1 = oml_iris[:, ["Sepal_Length", "Petal_Length"]]
iris_projected1.head(3)
# Select all rows with columns whose indices are in the range [1, 4).
iris_projected2 = oml_iris[:, 1:4]
iris_projected2.head(3)
# Select all rows with columns of oml.String data type.
iris_projected3 = oml_iris.select_types(include=[oml.String])
iris_projected3.head(3)
この例のリスト
>>> # Select all rows with specified column names.
... iris_projected1 = oml_iris[:, ["Sepal_Length", "Petal_Length"]]
>>> iris_projected1.head(3)
Sepal_Length Petal_Length
0 5.1 1.4
1 4.9 1.4
2 4.7 1.3
>>>
>>> # Select all rows with columns whose indices are in range [1, 4).
... iris_projected2 = oml_iris[:, 1:4]
>>> iris_projected2.head(3)
Sepal_Width Petal_Length Petal_Width
0 3.5 1.4 0.2
1 3.0 1.4 0.2
2 3.2 1.3 0.2
>>>
>>> # Select all rows with columns of oml.String data type.
... iris_projected3 = oml_iris.select_types(include=[oml.String])
>>> iris_projected3.head(3)
Species
0 setosa
1 setosa
2 setosa
値によるデータの選択
例7-3 値によるデータの選択
この例では、oml_iris
をフィルタ処理してiris_of_filtered1
を生成します。これには、花弁の長さが1.5より短いもので、Sepal_Length列およびPetal_Length列にあるoml_iris
の行の値が含まれます。この例ではさらに、条件を使用してデータをフィルタ処理し、花弁の長さが1.5より短いか、または萼片の長さが5.0であるoml_iris
の値がoml_iris_filtered2
に含まれ、花弁の長さが1.5より短く、かつ萼片の長さが5.0より長いoml_iris
の値がoml_iris_filtered3
に含まれるようにします。
# Select sepal length and petal length where petal length
# is less than 1.5.
oml_iris_filtered1 = oml_iris[oml_iris["Petal_Length"] < 1.5,
["Sepal_Length", "Petal_Length"]]
len(oml_iris_filtered1)
oml_iris_filtered1.head(3)
### Using the AND and OR conditions in filtering.
# Select all rows in which petal length is less than 1.5 or sepal length
# sepal length is 5.0.
oml_iris_filtered2 = oml_iris[(oml_iris["Petal_Length"] < 1.5) |
(oml_iris["Sepal_Length"] == 5.0), :]
len(oml_iris_filtered2)
oml_iris_filtered2.head(3)
# Select all rows in which petal length is less than 1.5 and
# sepal length is larger than 5.0.
oml_iris_filtered3 = oml_iris[(oml_iris["Petal_Length"] < 1.5) &
(oml_iris["Sepal_Length"] > 5.0), :]
len(oml_iris_filtered3)
oml_iris_filtered3.head()
この例のリスト
>>> # Select sepal length and petal length where petal length
... # is less than 1.5.
... oml_iris_filtered1 = oml_iris[oml_iris["Petal_Length"] < 1.5,
... ["Sepal_Length", "Petal_Length"]]
>>> len(oml_iris_filtered1)
24
>>> oml_iris_filtered1.head(3)
Sepal_Length Petal_Length
0 5.1 1.4
1 4.9 1.4
2 4.7 1.3
>>>
>>> ### Using the AND and OR conditions in filtering.
... # Select all rows in which petal length is less than 1.5 or
... # sepal length is 5.0.
... oml_iris_filtered2 = oml_iris[(oml_iris["Petal_Length"] < 1.5) |
... (oml_iris["Sepal_Length"] == 5.0), :]
>>> len(oml_iris_filtered2)
30
>>> oml_iris_filtered2.head(3)
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
>>>
>>> # Select all rows in which petal length is less than 1.5
... # and sepal length is larger than 5.0.
... oml_iris_filtered3 = oml_iris[(oml_iris["Petal_Length"] < 1.5) &
... (oml_iris["Sepal_Length"] > 5.0), :]
>>> len(oml_iris_filtered3)
7
>>> oml_iris_filtered3.head()
Sepal_Length Sepal_Width Petal_Length Petal_Width Species
0 5.1 3.5 1.4 0.2 setosa
1 5.8 4.0 1.2 0.2 setosa
2 5.4 3.9 1.3 0.4 setosa
3 5.1 3.5 1.4 0.3 setosa
4 5.2 3.4 1.4 0.2 setosa
親トピック: データの準備