Kỳ vọng có điều kiện

Nhập môn Data Quality với Great Expectations

Davina Moossazadeh

Data Scientist

Kỳ vọng có điều kiện là gì?

Kỳ vọng có điều kiện - Áp dụng cho một tập con dữ liệu

Vì sao? Vì một số biến phụ thuộc vào giá trị của biến khác

Ví dụ:

  • Một Expectation rằng cột star_rating phải bằng 0 cho mọi hàng có review_count bằng 0
1 https://docs.greatexpectations.io/docs/core/customize_expectations/expectation_conditions/
Nhập môn Data Quality với Great Expectations

Cú pháp cho Kỳ vọng có điều kiện

Dataset Expectation có thể chuyển thành Kỳ vọng có điều kiện với hai tham số bổ sung:

  1. row_condition
    • chuỗi biểu thức boolean xác định tập dữ liệu con để áp dụng Kỳ vọng có điều kiện
  2. condition_parser
    • chuỗi xác định cú pháp của row_condition
Nhập môn Data Quality với Great Expectations

Bộ phân tích điều kiện (condition parser)

Khi dùng pandas cho Kỳ vọng có điều kiện, đặt tham số này là "pandas"

expectation = gx.Expect...(
    **kwargs,
    condition_parser="pandas",
    row_condition=...
)
Nhập môn Data Quality với Great Expectations

Điều kiện hàng (row condition)

Cú pháp pandas
df["foo"] == 'Two Two'
df["foo"].notNull()
df["foo"] <= datetime.date(2023, 3, 13)
(df["foo"] < 5) & (df["foo"] >= 3.14)

df["foo"].str.startswith("bar")
Great Expectations row_condition
'foo == "Two Two"'
'foo.notNull()'
'foo <= datetime.date(2023, 3, 13)'
'(foo > 5) & (foo <= 3.14)'
'foo > 5 and foo <= 3.14'
'foo.str.startswith("bar")'
Nhập môn Data Quality với Great Expectations

Điều kiện hàng (row condition)

Quy tắc
  1. Không dùng dấu nháy đơn bên trong

    • row_condition="foo=='Two Two'"
    • row_condition='foo=="Two Two"' Dấu kiểm cho biết cú pháp của `row_condition` ở gạch đầu dòng thứ hai là đúng.
  2. Không xuống dòng bên trong

    • row_condition="""
      foo=="Two Two"
      """  
      
    • row_condition='foo=="Two Two"' Dấu kiểm cho biết cú pháp của `row_condition` ở gạch đầu dòng thứ hai là đúng.
Nhập môn Data Quality với Great Expectations

Ví dụ Expectation: xếp hạng sao

Không có điều kiện
expectation = gx.expectations.\
ExpectColumnValuesToBeBetween(
    column="price_usd",
    max_value=10,
)
validation_results = batch.validate(
    expect=expectation
)
print(validation_results.success)
False
Có điều kiện
expectation = gx.expectations.\
ExpectColumnValuesToBeBetween(
    column="price_usd",
    max_value=10,
    condition_parser='pandas',
    row_condition='mark_price_usd < 10',
)
validation_results = batch.validate(
    expect=expectation
)
print(validation_results.success)
True
Nhập môn Data Quality với Great Expectations

Ayo berlatih!

Nhập môn Data Quality với Great Expectations

Preparing Video For Download...