Perform Descriptive Statistics in Python

Prompt

Perform descriptive statistics
Python

Result

To perform descriptive statistics in Python, you can use the pandas library. Here's an example code that demonstrates how to calculate descriptive statistics for a given dataset:

import pandas as pd

def calculate_descriptive_statistics(data):
    """
    Calculate descriptive statistics for a given dataset.

    Parameters:
    data (list or pandas.DataFrame): The dataset for which to calculate descriptive statistics.

    Returns:
    pandas.DataFrame: A DataFrame containing the descriptive statistics.
    """
    # Convert the data to a pandas DataFrame if it's not already
    if not isinstance(data, pd.DataFrame):
        data = pd.DataFrame(data)

    # Calculate the descriptive statistics
    statistics = data.describe()

    return statistics

# Example usage
data = [1, 2, 3, 4, 5]
statistics = calculate_descriptive_statistics(data)
print(statistics.to_markdown())
Python

This code defines a function calculate_descriptive_statistics that takes a dataset as input and returns a DataFrame containing the descriptive statistics. The function first checks if the input data is already a DataFrame, and if not, converts it to one. Then, it uses the describe method of the DataFrame to calculate the descriptive statistics.

In the example usage, we create a list data with some sample data and pass it to the calculate_descriptive_statistics function. The resulting statistics DataFrame is then printed in markdown format using the to_markdown method.