Random Mathematical Operation on DataFrame Columns using Modin

  • Share this:

Code introduction


This function randomly selects a mathematical operation (sum, mean, max, min, product, or divide) from two input columns and performs the operation using the Modin library. The function first generates a random DataFrame, and then performs the selected operation on the specified columns.


Technology Stack : Modin, Pandas, NumPy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_modin_operation(column1, column2):
    import pandas as pd
    import numpy as np
    from modin.pandas import DataFrame
    
    # Generate random data
    df = DataFrame({
        'A': np.random.randint(1, 100, size=100),
        'B': np.random.rand(100)
    })
    
    # Randomly select an operation to perform
    operation = np.random.choice(['sum', 'mean', 'max', 'min', 'product', 'divide'])
    
    # Perform the operation on the selected columns
    if operation == 'sum':
        result = df['A'].sum()
    elif operation == 'mean':
        result = df['B'].mean()
    elif operation == 'max':
        result = df['A'].max()
    elif operation == 'min':
        result = df['B'].min()
    elif operation == 'product':
        result = df['A'].product(df['B'])
    elif operation == 'divide':
        result = df['A'].divide(df['B'], fill_value=0)
    
    return result