Recipe: How to randomly sample a Pandas DataFrame?
DATA MUNGING PYTHON PANDAS DATAFRAME PANDAS CHEATSHEET PANDAS DATAFRAME TUTORIAL

How to randomly sample a Pandas DataFrame?

This recipe helps you randomly sample a Pandas DataFrame
In [1]:
## How to randomly sample a Pandas DataFrame
def Kickstarter_Example_99():
    print()
    print(format('randomly sample a Pandas DataFrame','*^82'))
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    import pandas as pd
    import numpy as np
    # Create dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 52, 36, 24, 73],
                'preTestScore': [4, 24, 31, 2, 3],
                'postTestScore': [25, 94, 57, 62, 70]}
    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age',
                                           'preTestScore', 'postTestScore'])
    print(); print(df)
    # Select a random subset of 2 without replacement
    print(); print(df.take(np.random.permutation(len(df))[:2]))
    # Select a random subset of 4 without replacement
    print(); print(df.take(np.random.permutation(len(df))[:4]))
    # random sample of df    
    df1 = df.sample(3)
    print(); print(df1)
Kickstarter_Example_99()
************************randomly sample a Pandas DataFrame************************

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42             4             25
1      Molly  Jacobson   52            24             94
2       Tina       Ali   36            31             57
3       Jake    Milner   24             2             62
4        Amy     Cooze   73             3             70

  first_name last_name  age  preTestScore  postTestScore
1      Molly  Jacobson   52            24             94
3       Jake    Milner   24             2             62

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42             4             25
2       Tina       Ali   36            31             57
1      Molly  Jacobson   52            24             94
3       Jake    Milner   24             2             62

  first_name last_name  age  preTestScore  postTestScore
4        Amy     Cooze   73             3             70
1      Molly  Jacobson   52            24             94
2       Tina       Ali   36            31             57


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
2 developers from HP
1 developer from Aspiring Minds
1 developer from Ericsson
1 developer from ICFAI
1 developer from Kenya Commercial Bank
1 developer from MudraCircle
1 developer from Scotiabank
1 developer from Vodafone
1 developer from Altimetrik
1 developer from Barclays