Randomly Select and DataFrame a Fruit

  • Share this:

Code introduction


This function randomly selects an element from a predefined list and creates a pandas DataFrame containing that element, which is then returned.


Technology Stack : pandas

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_choice_from_list(arg1, arg2):
    import random
    import pandas as pd

    # Generate a random choice from a predefined list
    choices = ['apple', 'banana', 'cherry', 'date', 'elderberry']
    random_choice = random.choice(choices)

    # Create a DataFrame with the random choice
    df = pd.DataFrame({'Fruit': [random_choice]})

    # Return the DataFrame
    return df                
              
Tags: