Pandas data frame random sampling (both rows and columns)

I know how to randomly display multiple lines from a pandas data frame. Suppose I had a df data frame, and then, to get a fraction of the lines, I can do:

df_sample = df.sample(frac=0.007)

However, I need random rows as above, and also random columns from the above data frame.

Df is currently 56Kx8.5k. If I want to say 500x1000, where both 500 and 1000 are randomly selected, how to do this?

I think one approach will do something like

df.columns to get a list of column names.

Then do some random index selection on this column list and use random indexes to filter the remaining columns?

+4
1

sample , :

df.sample(n=500).sample(n=1000, axis=1)

, = 0, .

+7

All Articles