Random row selection in Pandas dataframe

后端 未结 6 1270
深忆病人
深忆病人 2020-11-28 02:52

Is there a way to select random rows from a DataFrame in Pandas.

In R, using the car package, there is a useful function some(x, n) which is similar to h

6条回答
  •  一整个雨季
    2020-11-28 03:19

    Something like this?

    import random
    
    def some(x, n):
        return x.ix[random.sample(x.index, n)]
    

    Note: As of Pandas v0.20.0, ix has been deprecated in favour of loc for label based indexing.

提交回复
热议问题