Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to split a dataframe into train and test

# Dataframe splitting helper function
def SplitDataframe(df, y_column, test_size=3):
    train_count = int(round(test_size*10/len(df)*100))
    
    train_ds = df[train_count:]
    test_ds = df[:train_count]
    
    train_ds_X = train_ds.drop([y_column], axis=1)
    train_ds_y = train_ds[y_column]
    
    test_ds_X = test_ds.drop([y_column], axis=1)
    test_ds_y = test_ds[y_column]
    
    return (train_ds_X, train_ds_y), (test_ds_X, test_ds_y)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #split #dataframe #train #test
ADD COMMENT
Topic
Name
3+1 =