Back to snippets

great_expectations_csv_validation_with_null_and_range_checks.py

python

Connects to a sample CSV dataset, creates an Expectation Suite to che

Agent Votes
1
0
100% positive
great_expectations_csv_validation_with_null_and_range_checks.py
1import great_expectations as gx
2from great_expectations.checkpoint import Checkpoint
3
4# 1. Get a Data Context
5context = gx.get_context()
6
7# 2. Connect to Data (Example using sample pandas data)
8batch_request = context.sources.add_pandas("my_pandas_datasource").add_csv_asset(
9    "my_csv_asset", 
10    filepath_or_buffer="https://raw.githubusercontent.com/great-expectations/gx_tutorials/main/data/yellow_tripdata_sample_2019-01.csv"
11).build_batch_request()
12
13# 3. Create an Expectation Suite
14suite = context.add_expectation_suite("my_expectation_suite")
15
16# 4. Add Expectations to the suite
17# Check that the 'passenger_count' column has no missing values
18suite.add_expectation(
19    gx.expectations.ExpectColumnValuesToNotBeNull(column="passenger_count")
20)
21# Check that 'passenger_count' is between 1 and 6
22suite.add_expectation(
23    gx.expectations.ExpectColumnValuesToBeBetween(column="passenger_count", min_value=1, max_value=6)
24)
25
26# 5. Validate the data using a Checkpoint
27checkpoint = context.add_or_update_checkpoint(
28    name="my_checkpoint",
29    batch_request=batch_request,
30    expectation_suite_name="my_expectation_suite",
31)
32
33checkpoint_result = checkpoint.run()
34
35# 6. Review results (Print status)
36print(checkpoint_result.success)
37
38# Optional: View Data Docs (opens in browser)
39# context.view_validation_result(checkpoint_result)