การใช้งาน PySpark DataFrames

Big Data Fundamentals with PySpark

Upendra Devisetty

Science Analyst, CyVerse

ตัวดำเนินการ DataFrame ใน PySpark

  • การดำเนินการบน DataFrame: Transformations และ Actions

  • DataFrame Transformations:

    • select(), filter(), groupby(), orderby(), dropDuplicates() และ withColumnRenamed()
  • DataFrame Actions:

    • printSchema(), head(), show(), count(), columns และ describe()

    หมายเหตุ: printSchema() เป็นเมธอดสำหรับ Spark dataset/dataframe ทุกชนิด ไม่ใช่ action

Big Data Fundamentals with PySpark

การใช้งาน select() และ show()

  • transformation select() ใช้เลือกเฉพาะคอลัมน์ที่ต้องการจาก DataFrame
df_id_age = test.select('Age')
  • action show() แสดง 20 แถวแรกของ DataFrame
df_id_age.show(3)
+---+
|Age|
+---+
| 17|
| 17|
| 17|
+---+
only showing top 3 rows
Big Data Fundamentals with PySpark

การใช้งาน filter() และ show()

  • transformation 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
Big Data Fundamentals with PySpark

การใช้งาน 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
Big Data Fundamentals with PySpark

Transformation 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
Big Data Fundamentals with PySpark

dropDuplicates()

  • dropDuplicates() ใช้ลบแถวที่ซ้ำกันออกจาก DataFrame
test_df_no_dup = test_df.select('User_ID','Gender', 'Age').dropDuplicates()
test_df_no_dup.count()
5892
Big Data Fundamentals with PySpark

Transformation 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|
+-------+---+---+
Big Data Fundamentals with PySpark

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)
Big Data Fundamentals with PySpark

action columns

  • columns ใช้แสดงรายชื่อคอลัมน์ทั้งหมดของ DataFrame
test_df.columns
['User_ID', 'Gender', 'Age']
Big Data Fundamentals with PySpark

action 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|
+-------+------------------+------+------------------+
Big Data Fundamentals with PySpark

มาฝึกกันเถอะ!

Big Data Fundamentals with PySpark

Preparing Video For Download...