Randomly Select Columns from Pandas DataFrame

  • Share this:

Code introduction


This function randomly selects a specified number of columns from a Pandas DataFrame and returns a new DataFrame.


Technology Stack : Pandas, random.sample

Code Type : Pandas DataFrame operation

Code Difficulty : Intermediate


                
                    
def random_dataframe_columns(dataframe, num_columns):
    """
    Selects random columns from a Pandas DataFrame.
    """
    import pandas as pd
    import random

    # Ensure the number of columns requested is not greater than the available columns
    num_columns = min(num_columns, len(dataframe.columns))
    
    # Randomly select column names
    selected_columns = random.sample(dataframe.columns, num_columns)
    
    # Return the selected columns
    return dataframe[selected_columns]