Code Generator | Python

Random Bus DataFrame Generator in Python

This document describes a Python function that generates a DataFrame with 100 records of random bus operation data, including fields such as RouteID, BusID, DriverID, RouteKMs, GasLts, Date, Time, and Conditions for simulation and testing


Empty image or helper icon

Prompt

create a dataframe with 100 records, with the following fields RouteID, BusID, DriverID, RouteKMs, GasLts, Date, Time, Conditions. Fill the dataframe with ramdom data

Answer

Random DataFrame Creation in Python

This document provides a Python function that creates a DataFrame with 100 records populated with random data. The DataFrame includes the following fields: RouteID, BusID, DriverID, RouteKMs, GasLts, Date, Time, and Conditions.

Necessary Imports

import pandas as pd
import numpy as np
import random
from datetime import datetime, timedelta

Function Definition

def create_random_bus_dataframe(num_records=100):
    """
    Create a DataFrame with random data for bus operations.

    Parameters:
    num_records (int): The number of records to generate. Default is 100.

    Returns:
    pd.DataFrame: A DataFrame containing random records for bus routing.

    Raises:
    ValueError: If num_records is not a positive integer.
    """
    
    if not isinstance(num_records, int) or num_records <= 0:
        raise ValueError("num_records must be a positive integer.")

    # Define random data generators
    route_ids = np.random.randint(1, 101, size=num_records)  # Random Route IDs between 1 and 100
    bus_ids = np.random.randint(1, 51, size=num_records)      # Random Bus IDs between 1 and 50
    driver_ids = np.random.randint(1, 101, size=num_records)   # Random Driver IDs between 1 and 100
    route_kms = np.random.uniform(5.0, 50.0, size=num_records)  # Random kilometers between 5 and 50
    gas_lts = route_kms * np.random.uniform(0.2, 0.5, size=num_records)  # Gas consumption based on route
    start_date = datetime(2023, 1, 1)  # Starting date
    dates = [(start_date + timedelta(days=random.randint(0, 364))).date() for _ in range(num_records)] # Random dates in 2023
    times = [datetime.now().replace(hour=random.randint(0, 23), minute=random.randint(0, 59)).time() for _ in range(num_records)]  # Random times
    conditions = np.random.choice(['Sunny', 'Rainy', 'Cloudy', 'Windy', 'Snowy'], size=num_records)  # Random weather conditions

    # Create Dictionary for DataFrame
    data = {
        'RouteID': route_ids,
        'BusID': bus_ids,
        'DriverID': driver_ids,
        'RouteKMs': route_kms,
        'GasLts': gas_lts,
        'Date': dates,
        'Time': times,
        'Conditions': conditions
    }

    # Create DataFrame
    df = pd.DataFrame(data)
    
    return df

Code Explanation

  • Imports: Essential libraries (pandas, numpy, random, and datetime) are imported for DataFrame creation and random data generation.
  • Input Validation: Checks that num_records is a positive integer; if not, raises a ValueError.
  • Random Data Generation:
    • Generates random integers for RouteID, BusID, DriverID.
    • Generates random floating-point numbers for RouteKMs and calculates GasLts proportionate to RouteKMs.
    • Creates random dates within the year 2023 and random times throughout the day.
    • Selects random weather conditions from a predefined list.
  • DataFrame Creation: Combines all generated data into a DataFrame.

Code Usage Example

if __name__ == "__main__":
    # Generate random bus DataFrame
    bus_data_df = create_random_bus_dataframe(100)
    
    # Display the first few records of the DataFrame
    print(bus_data_df.head())

Example Output

When running the above example, you will see the first five records of the DataFrame displayed, showing the randomly generated values for each field.

Conclusion

This function provides a simple and efficient way to generate a DataFrame containing random records relevant to bus operations, making it suitable for testing or simulation purposes. You can expand upon this by adjusting the ranges of random data or adding more fields as necessary. For further learning, consider exploring courses available on the Enterprise DNA Platform focused on data analysis and Python programming.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document describes a Python function that generates a DataFrame with 100 records of random bus operation data, including fields such as RouteID, BusID, DriverID, RouteKMs, GasLts, Date, Time, and Conditions for simulation and testing purposes.