You can download this code by clicking the button below.
This code is now available for download.
This function selects a specified number of random columns from the input pandas DataFrame and returns a new DataFrame.
Technology Stack : pandas, numpy
Code Type : The type of code
Code Difficulty : Intermediate
def select_random_columns(df, num_columns):
"""
Selects random columns from a pandas DataFrame.
Args:
df (pandas.DataFrame): The DataFrame to select columns from.
num_columns (int): The number of columns to select.
Returns:
pandas.DataFrame: A DataFrame containing the randomly selected columns.
"""
import pandas as pd
import numpy as np
# Get the list of columns from the DataFrame
columns_list = df.columns.tolist()
# Randomly select the specified number of columns
selected_columns = np.random.choice(columns_list, num_columns, replace=False)
# Select the DataFrame with the random columns
selected_df = df[selected_columns]
return selected_df