CSVファイルの読み込み

Spark で CSV ファイルを読み込み、DataFrame として扱うまでの流れを整理します。

1. サンプルデータの読み込み

まず、先ほど作成したsales.csvを読み込んでみましょう。PySparkシェルで以下のコードを実行してください。

df = spark.read.csv("sales.csv", header=True, inferSchema=True)

header=True は1行目をカラム名として使用するオプション、inferSchema=True はデータ型を自動推定するオプションです。

読み込んだデータの内容を確認します。

df.show()

以下のような実行結果が表示されます。

+--------+-------------+----------+--------+----------+----------+------+
|order_id|customer_name|product_id|quantity|unit_price|order_date|region|
+--------+-------------+----------+--------+----------+----------+------+
|  ORD001|     田中太郎|      P001|       1|    120000|2025-01-05|  東京|
|  ORD002|     鈴木花子|      P002|       2|      3000|2025-01-05|  大阪|
|  ORD003|     山田一郎|      P003|       3|      2500|2025-01-06|  東京|
|  ORD004|     佐藤美咲|      P004|       5|      1500|2025-01-06|  福岡|
|  ORD005|     高橋健太|      P005|       1|      8000|2025-01-07|  東京|
|  ORD006|     田中太郎|      P002|       1|      3500|2025-01-07|  東京|
|  ORD007|     鈴木花子|      P004|       2|      4000|2025-01-08|  大阪|
|  ORD008|     山田一郎|      P001|       1|     35000|2025-01-08|  東京|
|  ORD009|     佐藤美咲|      P002|       3|      2800|2025-01-09|  福岡|
|  ORD010|     高橋健太|      P003|      10|      1200|2025-01-09|  東京|
+--------+-------------+----------+--------+----------+----------+------+

2. データの確認

読み込んだDataFrameの構造や内容を確認するためのメソッドを紹介します。

2.1 カラム名とデータ型の確認(printSchema)

DataFrameのスキーマ(カラム名とデータ型の構造)を確認するには printSchema() を使用します。

df.printSchema()

以下のような実行結果が表示されます。

root
 |-- order_id: string (nullable = true)
 |-- customer_name: string (nullable = true)
 |-- product_id: string (nullable = true)
 |-- quantity: integer (nullable = true)
 |-- unit_price: integer (nullable = true)
 |-- order_date: string (nullable = true)
 |-- region: string (nullable = true)

inferSchema=True により、quantity と unit_price が自動的に integer 型として推定されていることがわかります。文字列のカラムは string 型になっています。

その他にも、データの件数や基本統計量を確認するメソッドがあります。それぞれ実行してみましょう。

2.2 行数の取得(count)

count() でDataFrameの行数を取得できます。

df.count()

以下のような実行結果が表示されます。

10

CSVファイルのヘッダー行を除いた10件のデータが読み込まれていることがわかります。

2.3 カラム名の一覧(columns)

columns でDataFrameのカラム名をリストとして取得できます。

df.columns

以下のような実行結果が表示されます。

['order_id', 'customer_name', 'product_id', 'quantity', 'unit_price', 'order_date', 'region']

2.4 基本統計量の確認(describe)

describe() で数値カラムの基本統計量(件数、平均、標準偏差、最小値、最大値)を確認できます。

df.describe().show()

以下のような実行結果が表示されます。

+-------+--------+-------------+----------+--------+------------------+----------+------+
|summary|order_id|customer_name|product_id|quantity|        unit_price|order_date|region|
+-------+--------+-------------+----------+--------+------------------+----------+------+
|  count|      10|           10|        10|      10|                10|        10|    10|
|   mean|    null|         null|      null|     2.9|           20150.0|      null|  null|
| stddev|    null|         null|      null|   2.77|           37528.8|      null|  null|
|    min|  ORD001|     佐藤美咲|      P001|       1|              1200|2025-01-05|  大阪|
|    max|  ORD010|     高橋健太|      P005|      10|            120000|2025-01-09|  福岡|
+-------+--------+-------------+----------+--------+------------------+----------+------+

数値カラム(quantity、unit_price)については平均や標準偏差が計算されていますが、文字列カラムについてはnullと表示されます。min・max は文字列カラムでも辞書順で算出されます。

📝 inferSchemaの注意点
inferSchema=True はデータ全体を一度読み込んで型を推定するため、大規模なデータでは処理時間が増加します。本番環境では schema オプションで明示的にスキーマを指定することを推奨します。