Random Row Selection from Vaex DataFrame

  • Share this:

Code introduction


This function randomly selects rows from a given Vaex DataFrame.


Technology Stack : Vaex

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import vaex as vx

def random_select_rows(df, n=5):
    """
    Selects random rows from a Vaex DataFrame.

    Args:
        df (vx.DataFrame): The Vaex DataFrame to select rows from.
        n (int): The number of rows to select. Default is 5.

    Returns:
        vx.DataFrame: A DataFrame containing the randomly selected rows.
    """
    # Generate a random permutation of the DataFrame's index
    permutation = df.index.random_permutation()
    # Select the first n rows from the permutation
    selected_indices = permutation[:n]
    # Use the selected indices to select the corresponding rows from the DataFrame
    selected_df = df.loc[selected_indices]
    return selected_df                
              
Tags: