Biến đổi

Introduction to Data Engineering

Vincent Vankrunkelsven

Data Engineer, DataCamp

Các kiểu biến đổi

customer_id email state created_at
1 [email protected] New York 2019-01-01 07:00:00

 

  • Chọn thuộc tính (ví dụ: 'email')
  • Quy đổi mã giá trị (ví dụ: 'New York' -> 'NY')
  • Kiểm tra dữ liệu (ví dụ: định dạng ngày ở 'created_at')
  • Tách một cột thành nhiều cột
  • Join từ nhiều nguồn
Introduction to Data Engineering

Ví dụ: tách (Pandas)

customer_id email username domain
1 [email protected] jane.doe theweb.com
customer_df # Pandas DataFrame với dữ liệu khách hàng

# Tách cột email thành 2 cột tại ký tự '@'
split_email = customer_df.email.str.split("@", expand=True)

# Lúc này, split_email sẽ có 2 cột: cột đầu chứa phần trước @, # cột thứ hai chứa phần sau @ # Tạo 2 cột mới từ DataFrame kết quả. customer_df = customer_df.assign( username=split_email[0], domain=split_email[1], )
Introduction to Data Engineering

Biến đổi với PySpark

Trích xuất dữ liệu vào PySpark

import pyspark.sql

spark = pyspark.sql.SparkSession.builder.getOrCreate()

spark.read.jdbc("jdbc:postgresql://localhost:5432/pagila",
"customer",
properties={"user":"repl","password":"password"})
Introduction to Data Engineering

Ví dụ: join

Một bảng ratings mới

customer_id film_id rating
1 2 1
2 1 5
2 2 3
... ... ...

Bảng customer

customer_id first_name last_name ...
1 Jane Doe ...
2 Joe Doe ...
... ... ... ...

 

customer_id trùng với bảng ratings

Introduction to Data Engineering

Ví dụ: join (PySpark)

customer_df # PySpark DataFrame với dữ liệu khách hàng
ratings_df # PySpark DataFrame với dữ liệu đánh giá

# Groupby ratings ratings_per_customer = ratings_df.groupBy("customer_id").mean("rating")
# Join theo customer ID customer_df.join( ratings_per_customer, customer_df.customer_id==ratings_per_customer.customer_id )
Introduction to Data Engineering

Cùng luyện tập nào!

Introduction to Data Engineering

Preparing Video For Download...