How do I create test and train samples from one dataframe with pandas?

I have a fairly large dataset in the form of a dataframe and I was wondering how I would be able to split the dataframe into two random samples (80% and 20%) for training and testing.

Thanks!

I would just use numpy's randn:

    In [11]: df = pd.DataFrame(np.random.randn(100, 2))

    In [12]: msk = np.random.rand(len(df)) < 0.8

    In [13]: train = df[msk]

    In [14]: test = df[~msk]

And just to see this has worked:

    In [15]: len(test)
    Out[15]: 21

    In [16]: len(train)
    Out[16]: 79

From: stackoverflow.com/q/24147278

Back to homepage or read more recommendations: