Random Sample of a subset of a dataframe in Pandas

后端 未结 3 2079
北荒
北荒 2020-12-10 23:58

Say i have a dataframe with 100,000 entries and want to split it into 100 sections of 1000 entries.

How do i take a random sample of say size 50 of just one of the

3条回答
  •  鱼传尺愫
    2020-12-11 00:34

    This is a nice place for recursion.

    def main2():
        rows = 8  # say you have 8 rows, real data will need len(rows) for int
        rands = []
        for i in range(rows):
            gen = fun(rands)
            rands.append(gen)
        print(rands)  # now range through random values
    
    
    def fun(rands):
        gen = np.random.randint(0, 8)
        if gen in rands:
            a = fun(rands)
            return a
        else: return gen
    
    
    if __name__ == "__main__":
        main2()
    

    output: [6, 0, 7, 1, 3, 5, 4, 2]

提交回复
热议问题