Data Processing and Analysis
This section demonstrates the complete Aura DataFrame processing workflow, including filtering, column calculation, grouping and aggregation, and sorting.
Scenario
In a sales data analysis scenario, it is necessary to calculate summary metrics for order details up to a specific cutoff date. The data should be grouped by return flag and order status. The metrics to be calculated include the total quantity, total base price, total discounted price, total tax-inclusive price, average value of each metric, and number of orders. By leveraging the chain operations of Aura DataFrame, you can sequentially perform filtering, derived column calculation, grouping and aggregation, and sorting. This achieves a data processing workflow equivalent to SQL, providing data support for sales reports and business decisions.
Prerequisites
- A table has been created and data has been written to it. For details, see Creating a Table and Writing Data to the Table.
- The Python 3.11 has been installed.
Example
This example performs Aura DataFrame operations such as filtering, derived column calculation, grouping and aggregation, and sorting on the sales_line_items table to demonstrate the complete data processing and analysis workflow.
- Import dependencies and connect to the AuraJobV2 endpoint.
- Load the sales_line_items dataset.
- Filter records where ship_date is before the cutoff date.
- Calculate derived columns such as the discounted price and tax-inclusive price.
- Group the data by return_flag and line_status and then perform aggregate calculations (sum, avg, and count) within the groups.
- Sort the data by return_flag and line_status.
- Execute the query and print the results.
- Disable the AuraJobV2 endpoint connection.
import os from datetime import datetime, timedelta from aura_frame.multimodal import ai_lake target_database = "test" #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata. #Connect to the AuraJobV2 endpoint. For details about the parameters, see Establishing a Connection with the AuraJobV2 Endpoint. con = ai_lake.connect( aura_endpoint=os.getenv("aura_endpoint"), # Endpoint where the AuraJobV2 endpoint is located aura_endpoint_id=os.getenv("aura_endpoint_id"), # AuraJobV2 endpoint ID aura_endpoint_name=os.getenv("aura_endpoint_name"),# AuraJobV2 endpoint name aura_workspace_id=os.getenv("aura_workspace_id"), # Workspace ID lf_catalog_name=os.getenv("lf_catalog_name"), # LakeFormation Catalog name access_key=os.getenv("access_key"), # Access Key ID (AK) secret_key=os.getenv("secret_key"), # Secret Key (SK) default_database=target_database, # LakeFormation database name use_single_cn_mode=True, # Whether to enable the single-CN mode logging_level=logging.INFO # Set the log level. ) try: # Load the dataset. ds = con.load_dataset("sales_line_items", database=target_database) cutoff = datetime(1998, 12, 1) - timedelta(days=90) # Filtering: Filter out rows that do not meet the conditions. q = ds.filter(expr=ds.ship_date <= cutoff) # Calculate derived columns. discount_price = ds.unit_price * (1 - ds.discount_rate) charge = discount_price * (1 + ds.tax_rate) # Group by and aggregate. q = q.groupby(["return_flag", "line_status"]) q = q.aggregate( sum_qty=ds.quantity.sum(), sum_base_price=ds.unit_price.sum(), sum_disc_price=discount_price.sum(), sum_charge=charge.sum(), avg_qty=ds.quantity.mean(), avg_price=ds.unit_price.mean(), avg_disc=ds.discount_rate.mean(), count_order=ds.count(), ) # Sort. q = q.order_by(["return_flag", "line_status"]) # Trigger execution. df = q.execute() print(df) finally: con.close()
Helpful Links
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot