PySpark の DataFrame と対話する

PySparkで学ぶBig Data入門

Upendra Devisetty

Science Analyst, CyVerse

PySpark の DataFrame 演算子

  • DataFrame の操作: 変換とアクション

  • DataFrame の変換:

    • select(), filter(), groupby(), orderby(), dropDuplicates(), withColumnRenamed()
  • DataFrame のアクション:

    • printSchema(), head(), show(), count(), columns, describe()

    訂正: printSchema() は Spark の任意のデータセット/データフレームのメソッドであり、アクションではありません

PySparkで学ぶBig Data入門

select() と show() の操作

  • select() 変換は DataFrame の列を抽出します
df_id_age = test.select('Age')
  • show() アクションは先頭20行を表示します
df_id_age.show(3)
+---+
|Age|
+---+
| 17|
| 17|
| 17|
+---+
only showing top 3 rows
PySparkで学ぶBig Data入門

filter() と show() の操作

  • filter() 変換は条件に基づいて行を抽出します
new_df_age21 = new_df.filter(new_df.Age > 21)
new_df_age21.show(3)
+-------+------+---+
|User_ID|Gender|Age|
+-------+------+---+
|1000002|     M| 55|
|1000003|     M| 26|
|1000004|     M| 46|
+-------+------+---+
only showing top 3 rows
PySparkで学ぶBig Data入門

groupby() と count() の操作

  • groupby() は変数でグループ化します
test_df_age_group = test_df.groupby('Age')
test_df_age_group.count().show(3)
+---+------+
|Age| count|
+---+------+
| 26|219587|
| 17|     4|
| 55| 21504|
+---+------+
only showing top 3 rows
PySparkで学ぶBig Data入門

orderby() の変換

  • orderby() は1つ以上の列で DataFrame を並べ替えます
test_df_age_group.count().orderBy('Age').show(3)
+---+-----+
|Age|count|
+---+-----+
|  0|15098|
| 17|    4|
| 18|99660|
+---+-----+
only showing top 3 rows
PySparkで学ぶBig Data入門

dropDuplicates()

  • dropDuplicates() は DataFrame の重複行を削除します
test_df_no_dup = test_df.select('User_ID','Gender', 'Age').dropDuplicates()
test_df_no_dup.count()
5892
PySparkで学ぶBig Data入門

withColumnRenamed の変換

  • withColumnRenamed() は DataFrame の列名を変更します
test_df_sex = test_df.withColumnRenamed('Gender', 'Sex')
test_df_sex.show(3)
+-------+---+---+
|User_ID|Sex|Age|
+-------+---+---+
|1000001|  F| 17|
|1000001|  F| 17|
|1000001|  F| 17|
+-------+---+---+
PySparkで学ぶBig Data入門

printSchema()

  • printSchema() は DataFrame の列の型を表示します
test_df.printSchema()
 |-- User_ID: integer (nullable = true)
 |-- Product_ID: string (nullable = true)
 |-- Gender: string (nullable = true)
 |-- Age: string (nullable = true)
 |-- Occupation: integer (nullable = true)
 |-- Purchase: integer (nullable = true)
PySparkで学ぶBig Data入門

columns アクション

  • columns は DataFrame の列名を一覧表示します
test_df.columns
['User_ID', 'Gender', 'Age']
PySparkで学ぶBig Data入門

describe() アクション

  • describe() は数値列の要約統計量を計算します
test_df.describe().show()
+-------+------------------+------+------------------+
|summary|           User_ID|Gender|               Age|
+-------+------------------+------+------------------+
|  count|            550068|550068|            550068|
|   mean|1003028.8424013031|  null|30.382052764385495|
| stddev|1727.5915855307312|  null|11.866105189533554|
|    min|           1000001|     F|                 0|
|    max|           1006040|     M|                55|
+-------+------------------+------+------------------+
PySparkで学ぶBig Data入門

Let's practice

PySparkで学ぶBig Data入門

Preparing Video For Download...