How do I create test and train samples from one dataframe with pandas?
scikit learn's train_test_split
is a good one - it will split both numpy arrays as dataframes.
from sklearn.model_selection import train_test_splittrain, test = train_test_split(df, test_size=0.2)
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.8In [13]: train = df[msk]In [14]: test = df[~msk]
And just to see this has worked:
In [15]: len(test)Out[15]: 21In [16]: len(train)Out[16]: 79
Pandas random sample will also work
train=df.sample(frac=0.8,random_state=200) #random state is a seed valuetest=df.drop(train.index)