Pandas random sample with remove

前端 未结 2 1284
忘掉有多难
忘掉有多难 2021-02-04 08:31

I\'m aware of DataFrame.sample(), but how can I do this and also remove the sample from the dataset? (Note: AFAIK this has nothing to do with sampling with repl

相关标签:
2条回答
  • 2021-02-04 08:51

    If your index is unique

    df = df.drop(df_subset.index)
    

    example

    df = pd.DataFrame(np.arange(10).reshape(-1, 2))
    

    sample

    df_subset = df.sample(2)
    df_subset
    


    drop

    df.drop(df_subset.index)
    

    0 讨论(0)
  • 2021-02-04 08:51

    pandas random sample :

    train=df.sample(frac=0.8,random_state=200)
    test=df.drop(train.index)
    
    0 讨论(0)
提交回复
热议问题