<jupyter_start><jupyter_text>![Impressions](https://PixelServer20190423114238.azurewebsites.net/api/impressions/MachineLearningNotebooks/work-with-data/dataprep/how-to-guides/subsetting-sampling.png)  Sampling and Subsetting Once a Dataflow has been created, it is possible to act on only a subset of the records contained in it. This can help when working with very large datasets or when only a portion of the records is truly relevant.  HeadThe `head` method will take the number of records specified, run them through the transformations in the Dataflow, and then return the result as a Pandas dataframe.<jupyter_code>import azureml.dataprep as dprep

dflow = dprep.read_csv('../data/crime_duplicate_headers.csv')
dflow.head(5)<jupyter_output><empty_output><jupyter_text>TakeThe `take` method adds a step to the Dataflow that will keep the number of records specified (counting from the beginning) and drop the rest. Unlike `head`, which does not modify the Dataflow, all operations applied on a Dataflow on which `take` has been applied will affect only the records kept.<jupyter_code>dflow_top_five = dflow.take(5)
dflow_top_five.to_pandas_dataframe()<jupyter_output><empty_output><jupyter_text>SkipIt is also possible to skip a certain number of records in a Dataflow, such that transformations are only applied after a specific point. Depending on the underlying data source, a Dataflow with a `skip` step might still have to scan through the data in order to skip past the records.<jupyter_code>dflow_skip_top_one = dflow_top_five.skip(1)
dflow_skip_top_one.to_pandas_dataframe()<jupyter_output><empty_output><jupyter_text>Take SampleIn addition to taking records from the top of the dataset, it's also possible to take a random sample of the dataset. This is done through the `take_sample(probability, seed=None)` method. This method will scan through all of the records available in the Dataflow and include them based on the probability specified. The `seed` parameter is optional. If a seed is not provided, a stable one is generated, ensuring that the results for a specific Dataflow remain consistent. Different calls to `take_sample` will receive different seeds.<jupyter_code>dflow_sampled = dflow.take_sample(0.1)
dflow_sampled.to_pandas_dataframe()<jupyter_output><empty_output><jupyter_text>`skip`, `take`, and `take_sample` can all be combined. With this, we can achieve behaviors like getting a random 10% sample fo the middle N records of a dataset.<jupyter_code>seed = 1
dflow_nested_sample = dflow.skip(1).take(5).take_sample(0.5, seed)
dflow_nested_sample.to_pandas_dataframe()<jupyter_output><empty_output><jupyter_text>Take Stratified SampleBesides sampling all by a probability, we also have stratified sampling, provided the strata and strata weights, the probability to sample each stratum with.This is done through the `take_stratified_sample(columns, fractions, seed=None)` method.For all records, we will group each record by the columns specified to stratify, and based on the stratum x weight information in `fractions`, include said record.Seed behavior is same as in `take_sample`.If a stratum is not specified or the record cannot be grouped by said stratum, we default the weight to sample by to 0 (it will not be included).The order of `fractions` must match the order of `columns`.<jupyter_code>fractions = {}
fractions[('ASSAULT',)] = 0.5
fractions[('BATTERY',)] = 0.2
fractions[('ARSON',)] = 0.5
fractions[('THEFT',)] = 1.0

columns = ['Primary Type']

single_strata_sample = dflow.take_stratified_sample(columns=columns, fractions = fractions, seed = 42)
single_strata_sample.head(5)<jupyter_output><empty_output><jupyter_text>Stratified sampling on multiple columns is also supported.<jupyter_code>fractions = {}
fractions[('ASSAULT', '560')] = 0.5
fractions[('BATTERY', '460')] = 0.2
fractions[('ARSON', '1020')] = 0.5
fractions[('THEFT', '820')] = 1.0

columns = ['Primary Type', 'IUCR']

multi_strata_sample = dflow.take_stratified_sample(columns=columns, fractions = fractions, seed = 42)
multi_strata_sample.head(5)<jupyter_output><empty_output><jupyter_text>Selecting PartitionsThe data produced by Dataflows is processed in partitions. How different data sources and formats are partitioned is guaranteed to be stable for a specific execution mode and version of azureml.dataprep. Usually, these partitions should not be interacted directly and instead higher-level APIs should be leveraged. In certain advanced scenarios, however, it can be useful to create a Dataflow that contains only a subset of the partitions of another. The `select_partitions` method can help accomplish this.<jupyter_code>partition_count = dflow.get_partition_count()
# We'll keep only even-numbered partitions
desired_partitions = [p for p in range(0, partition_count) if p % 2 == 0]
subset_dflow = dflow.select_partitions(desired_partitions)

subset_dflow.to_pandas_dataframe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07877683272579993, 'fraction_numerical': 0.013163223977318752, 'mean_word_length': 6.382660687593423, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '119199', 'license': 'MIT', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1431, 'n_words': 548, 'path': 'how-to-use-azureml/work-with-data/dataprep/how-to-guides/subsetting-sampling.ipynb', 'repo_name': 'alla15747/MachineLearningNotebooks', 'size': '4938'}