Package: choosy
choosyTable of ContentsInstallationUsageLicenseInstallationpip install choosyUsageSamplingchoosyis designed to make it easy to do selective sampling from a Pandas dataframe.
The main work is handled by theStructuredSamplerclass, which holds a dataframe and
optional information about informative columns.importpandasaspdimportchoosydf=pd.DataFrame({"category_col":["a"]*5+["b"]*5,"value_col":[1,2,3,4,5]*2,})sampler=choosy.StructuredSampler(df,)The most simple case is that you want to samplenrows from a dataframe.
Frankly, in that simple case you should just use the nativedf.sample()method, butchoosycan do that too:sampler.sample_data(n_sample=3)will return 3 random rows.The real use ofchoosycomes from the ability to sample rows based on values in a column.
Let's say we want to sample 2 rows with valueaincategory_coland 1 row with valueb.
Instead of an integer forn_sample, we use a dictionary mapping values to the number of samples from rows with that value.
In addition, we define abin_columnthat will be the column in which to find those values.sampler.sample_data(n_sample={'a':2,'b':1},bin_column="category_col")Now you will get three rows, one with valueband two with valuea.
Note that it will only sample from those values specified, which may mean that some rows with values other than keys in the dictionary are never potentially sampled.
If you specify abin_columnbutn_sampleis an integer, it will sample that many values from each unique value in thebin_column.If you want to sample based on the unique combination of values in multiple columns, you can specify a list ofbin_columnsand keys can be a tuple of sample selection values in the same order.
Alternatively, you can use a pandas Series whose index is the sample selection values and whose elements are the number of samples to take from that value.
The second case is useful if you have, for example, a dataframe with observations and second baseline dataframe that you want to sample from with the same frequencies based on some value.
Here, you can use thegroupbymethod to get the number of samples to take from each value.n_sample=df_obs.groupby(['selection_column_1','selection_column_2'])['selection_column_2'].count()]sampler=choosy.StructuredSampler(df_baseline)df_sample=sampler.sample_data(n_sample=n_sample,bin_column=['selection_column_1','selection_column_2'],)Counting and Repeat SamplingIn all cases above, thesample_datamethod returns a dataframe with the sampled rows.
In many applications, the main thing you want to know is the number of observations of some other value in the dataframe.
By adding acount_columnargument, to any of the situations described above, you get a dataframe with the sampled counts of the values in that column.
This is fully equivalent to doing agroupby().count()on the results of thesample_datamethod.```pythondf=pd.DataFrame({"category_col":["a"]*5+["b"]*5,"value_col":[1,2,3,4,5]*2,})sampler=choosy.StructuredSampler(df,count_column='value_col',)# Sample and get the list of observations of each value from category_col.sampler.sample_data(n_sample={'a':2,'b':1},count_column='value_col',bin_column='category_col',)To getn_repeatsample distributions of a given count variable, you can use thesample_repeatmethod.
You are required to specify acount_columnfor this method, and you can specify abin_columnandn_sampleas described above.
For example, to do 1000 repeats of the sampling above:sampler.sample_repeat(n_repeat=1000,n_sample={'a':2,'b':1},count_column='value_col',bin_column='category_col',)NotesIf you plan to always sample using the samebin_column, you can also specify this value in theStructuredSamplerconstructor.
This will save you from having to specify it in every call tosample_data.A count column can now be specified in the StructuredSampler initialization for simplicity.You can also specify aweight_columnin either the constructor or thesample_data/sample_repeatmethods to weight the sampling by the values in that column, which is passed through thepd.sample.choosycurrently requires sampling with replacement.Aseedcan be specified in the StructuredSampler initialization to make sampling reproducible.As of version 0.0.4, sampling has been optimized for speed by using numpy.random.choose on an array of indices.Licensechoosyis distributed under the terms of theBSD 3-Clauselicense.