# Download only a subset of a split

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%3E%5C
**Category:** 🤗Datasets\
**Created:** [April 3, 2022, 9:22am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410 "2022-04-03T09:22:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![morenolq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/morenolq/32/55542_2.png) [@morenolq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmorenolq)%5C
**Post date:** [April 3, 2022, 9:22am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F1 "2022-04-03T09:22:12Z")

</div>

Hi,

I was wondering if is there a way to download only part of the data of a dataset.  
In my specific case, I need to download only X samples from [oscar](https://huggingface.co/datasets/oscar) English split (X~100K samples).  
When I try to invoke the dataset builder it asks for \>1TB of space so I think it will download the full set of data at the beginning.

---

<div class="post-metadata">

**Author:** ![merve](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/merve/32/49809_2.png) [@merve](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmerve)%5C
**Post date:** [April 4, 2022, 10:36am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F2 "2022-04-04T10:36:51Z")

</div>

Hello 🤗

You can load a part of split by slicing:

`train_10_20_ds = datasets.load_dataset('bookcorpus', split='train[10:20]')`

You can refer to more ways of slicing and loading [here](https://huggingface.co/docs/datasets/loading#slice-splits).

---

<div class="post-metadata">

**Author:** ![morenolq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/morenolq/32/55542_2.png) [@morenolq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmorenolq)%5C
**Post date:** [April 4, 2022, 4:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F3 "2022-04-04T16:41:22Z")

</div>

Thank you, can I also use streaming mode to reach the same?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [April 7, 2022, 9:27am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F4 "2022-04-07T09:27:50Z")

</div>

Hi, let me just complete this: ` split='train[10:20]'` returns a slice of the data, but it still downloads everything.

If your dataset is too big, please use streaming mode. You can also slice your dataset in streaming mode, see the documentation here: [Stream](https://huggingface.co/docs/datasets/stream#split-dataset)

---

<div class="post-metadata">

**Author:** ![morenolq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/morenolq/32/55542_2.png) [@morenolq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmorenolq)%5C
**Post date:** [April 7, 2022, 12:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F5 "2022-04-07T12:27:27Z")

</div>

Thank you, that’s the case indeed.

---

<div class="post-metadata">

**Author:** ![vesuppi](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vesuppi](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvesuppi)%5C
**Post date:** [April 19, 2023, 9:40pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F6 "2023-04-19T21:40:50Z")

</div>

When I stream a dataset that’s too big, it always seems to get stuck after certain point (program hanging and not making progress), like after 10000 samples etc. After a long time it says something like reconnect to data host. Any reasons for that? Thanks!

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [May 2, 2023, 12:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F7 "2023-05-02T12:38:01Z")

</div>

It depends on the host. Some datasets are hosted on HF, but some others have their data files hosted on the original dataset author/platform. You can check how the dataset is loaded by checking its repository on HF. Which dataset did you try to load ?

---

<div class="post-metadata">

**Author:** ![lucasjin](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lucasjin](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flucasjin)%5C
**Post date:** [June 19, 2024, 2:00pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F8 "2024-06-19T14:00:57Z")

</div>

same issue here

i just need 10-20 parquet from 67069567 all…

i zabnit diwnload tehm all

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [June 21, 2024, 10:55am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F9 "2024-06-21T10:55:04Z")

</div>

Datasets are now generally hosted on HF, you can pass the `data_files=` argument to `load_dataset` to only load a subset of the data in the `datasets` lib 🙂

---

<div class="post-metadata">

**Author:** ![srinjoyMukherjee](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@srinjoyMukherjee](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FsrinjoyMukherjee)%5C
**Post date:** [November 20, 2024, 8:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F10 "2024-11-20T08:42:53Z")

</div>

thanks for the answer, but what if I want to just specify the number of files I want without actually listing their names, is it possible to do that? lets say I just want to download 20 data files without listing their names in data\_files

Thanks in advance

---

<div class="post-metadata">

**Author:** ![neonwatty](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/neonwatty/32/49082_2.png) [@neonwatty](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fneonwatty)%5C
**Post date:** [February 25, 2025, 2:50pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdownload-only-a-subset-of-a-split%2F16410%2F11 "2025-02-25T14:50:33Z")

</div>

Faced this issue as well so wrote a short script that pulls a hub dataset, creates a small sample of it, and pushes the sample data to the hub as a new dataset.

```python
def create_sample_dataset(full_dataset_name, sample_count=100, username="my-username", cache_dir="./dataset"):
    # Create a directory to save the sampled dataset
    os.makedirs(cache_dir, exist_ok=True)

    # Get the dataset name
    dataset_name = full_dataset_name.split("/")[-1]
    dataset_name_sample = f"{dataset_name}-sample-{sample_count}"

    # Load the dataset
    dataset = datasets.load_dataset(full_dataset_name, cache_dir=cache_dir)

    # Sample 100 rows from the training split (or modify for other splits)
    train_sample = dataset["train"].shuffle(seed=42).select(range(sample_count))
    test_sample = dataset["test"].shuffle(seed=42).select(range(sample_count))

    # Push to hub
    train_sample.push_to_hub(dataset_name_sample, split="train")
    print("INFO: Train split pushed to the hub successfully")

    test_sample.push_to_hub(dataset_name_sample, split="test")
    print("INFO: Test split pushed to the hub successfully")

```

The [full gist is here](https://gist.github.com/neonwatty/2205277501928f5945726e9e90950ae9).
