Adding Random Missing Values to a Column in Vaex DataFrame

  • Share this:

Code introduction


This function adds random missing values to a specified column in a Vaex DataFrame by creating a random mask.


Technology Stack : Vaex

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import vaex as vx

def random_missing_values(df, column_name, missing_ratio):
    """
    This function adds a random amount of missing values to a specified column in a Vaex DataFrame.
    """
    # Generate a random mask for missing values
    mask = vx.random.rand(len(df)) < missing_ratio
    # Apply the mask to the column to create missing values
    df[column_name] = df[column_name].mask(mask, vaex.nan)
    return df                
              
Tags: