Random row selection in Pandas dataframe
With pandas version 0.16.1
and up, there is now a DataFrame.sample
method built-in:
import pandasdf = pandas.DataFrame(pandas.np.random.random(100))# Randomly sample 70% of your dataframedf_percent = df.sample(frac=0.7)# Randomly sample 7 elements from your dataframedf_elements = df.sample(n=7)
For either approach above, you can get the rest of the rows by doing:
df_rest = df.loc[~df.index.isin(df_percent.index)]
Something like this?
import randomdef 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.
sample
As of v0.20.0, you can use pd.DataFrame.sample
, which can be used to return a random sample of a fixed number rows, or a percentage of rows:
df = df.sample(n=k) # k rowsdf = df.sample(frac=k) # int(len(df.index) * k) rows
For reproducibility, you can specify an integer random_state
, equivalent to using np.ramdom.seed
. So, instead of setting, for example, np.random.seed = 0
, you can:
df = df.sample(n=k, random_state=0)