You can download this code by clicking the button below.
This code is now available for download.
This function randomly shuffles the order of specified columns in a Vaex DataFrame.
Technology Stack : Vaex, NumPy, Pandas
Code Type : Function
Code Difficulty : Intermediate
import vaex as v
import numpy as np
import pandas as pd
import random
def randomize_columns(df, num_cols):
"""
Randomly shuffle the order of columns in a Vaex DataFrame.
Args:
- df (v.DataFrame): The input Vaex DataFrame.
- num_cols (int): The number of columns to shuffle.
Returns:
- v.DataFrame: A new Vaex DataFrame with shuffled columns.
"""
# Select random columns
cols_to_shuffle = df.columns[:num_cols]
# Shuffle the selected columns
shuffled_cols = random.sample(cols_to_shuffle, len(cols_to_shuffle))
# Create a new DataFrame with shuffled columns
new_df = df[shuffled_cols]
return new_df