Custom Data Summary Report Generator Using Prefect and Pandas

  • Share this:

Code introduction


This code defines a custom function that uses Prefect and Pandas libraries to generate a report with summary statistics of the provided data. It accepts a dictionary of data, converts it to a DataFrame, calculates descriptive statistics, and saves the results to a CSV file.


Technology Stack : Prefect, Pandas, DataFrame, describe, CSV, datetime

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
from datetime import datetime
import pandas as pd
from prefect import task, Flow

# Custom function using Prefect, Pandas, and datetime
def generate_report(data):
    """
    Generate a report with summary statistics of the provided data.
    """
    # Convert data to a DataFrame
    df = pd.DataFrame(data)
    
    # Calculate summary statistics
    summary_stats = df.describe()
    
    # Create a timestamp for the report
    report_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    # Save the summary statistics to a CSV file
    summary_stats.to_csv(f'report_{report_time}.csv')
    
    return f'Report generated at {report_time}'

# Prefect task
@task
def run_report(data):
    return generate_report(data)

# Example usage
if __name__ == "__main__":
    example_data = {'value': [10, 20, 30, 40, 50]}
    report = run_report(example_data)
    print(report)