How to use Sample in pandas
This article will explain in detail how to use Sample in pandas. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Sample
Sample is used to randomly select several rows or columns from the DataFrame.
Usage:
DataFrame.sample (n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)
Parameter function:
(1) n: the number of rows to be extracted
(2) frac: the proportion of rows extracted
Frac=0.8, for example, extracts 80% of them.
(3) replace: whether it is sampled back.
True: there is a return sampling
False: unreturned sampling
(4) weights: character index or probability array
(5) random_state: random number generator seed
(6) axis: select the row or column from which to extract data
Axis=0: extract rows
Axis=1: extract column
For example, five rows are randomly selected from the df:
Sample1 = df.sample (nasty 5) sample1
Take 60% of the rows randomly from df, and set random number seeds to get the same sample each time:
Sample2 = df.sample (frac=0.6,random_state=2) sample2
This is the end of the article on "how to use Sample in pandas". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.