與 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 資料集/DataFrame,但不屬於動作

使用 PySpark 的 Big Data 基礎

select() 與 show() 操作

  • select() 轉換會擷取 DataFrame 的欄位子集
df_id_age = test.select('Age')
  • show() 動作會印出 DataFrame 的前 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() 會依一或多個欄位排序 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() 會計算 DataFrame 數值欄位的摘要統計
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 基礎

一起來練習吧!

使用 PySpark 的 Big Data 基礎

Preparing Video For Download...