create a function that selects all numeric variables from a dataframe and plots histograms for each selected variable in python

You can create a function in Python that selects all numeric variables from a DataFrame and plots histograms for each selected variable. Here's an example function using pandas and matplotlib libraries:

main.py
import pandas as pd
import matplotlib.pyplot as plt

def plot_numeric_variables(df):
    # Select numeric variables from the DataFrame
    numeric_vars = df.select_dtypes(include='number')
    
    # Plot histograms for each numeric variable
    for column in numeric_vars.columns:
        plt.hist(numeric_vars[column])
        plt.title(f"Histogram of {column}")
        plt.xlabel(column)
        plt.ylabel("Frequency")
        plt.show()
443 chars
15 lines

To use this function, simply pass your DataFrame as an argument, like this:

main.py
# Assuming you have a DataFrame called 'df'
plot_numeric_variables(df)
71 chars
3 lines

This function will iterate through each numeric variable in the DataFrame and plot a separate histogram for each variable. The title of each histogram will be the name of the variable. The x-axis represents the values of the variable, and the y-axis represents the frequency of each value.

Make sure you have the pandas and matplotlib libraries installed before running this code.

related categories

gistlibby LogSnag