# Initializing splits from existing Dataset objects

**URL:** <https://discuss.huggingface.co/t/initializing-splits-from-existing-dataset-objects/16027>\
**Category:** 🤗Datasets\
**Created:** [March 24, 2022, 9:57am UTC](https://discuss.huggingface.co/t/initializing-splits-from-existing-dataset-objects/16027 "2022-03-24T09:57:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [March 24, 2022, 9:57am UTC](https://discuss.huggingface.co/t/initializing-splits-from-existing-dataset-objects/16027/1 "2022-03-24T09:57:05Z")

</div>

Hi,

I do have a dataset that looks like this:

 ![Screenshot 2022-03-24 at 10.52.00](https://us1.discourse-cdn.com/hellohellohello/original/2X/8/82d05867f012cb1656cae078047be007d19cb9a0.jpeg)

It consists of a single “train” split containing 2 columns, “image” and “label”. Each image has one of 4 possible labels: trainA, trainB, testA, testB.

What I’d like to do is turn this dataset into one that has 2 splits (“train” and “test”), each having 2 columns (“imageA” and “imageB”). How can I do this?

I can filter on them:

```auto
from datasets import load_dataset

dataset = load_dataset("huggan/grumpifycat")

train_examples_A = dataset.filter(lambda example: example['label'] == 2)['train']
train_examples_B = dataset.filter(lambda example: example['label'] == 3)['train']
test_examples_A = dataset.filter(lambda example: example['label'] == 0)['train']
test_examples_B = dataset.filter(lambda example: example['label'] == 1)['train']

```

Now I have 4 Dataset objects. Can I use those to instantiate a new DatasetDict with these objects?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [April 7, 2022, 9:32am UTC](https://discuss.huggingface.co/t/initializing-splits-from-existing-dataset-objects/16027/2 "2022-04-07T09:32:57Z")

</div>

Hi ! A `DatasetDict` is simply a python dictionary of datasets:

```python
train_dataset = concatenate_datasets([
    train_examples_A.rename_column("image", "imageA").remove_columns(["label"]),
    train_examples_B.rename_column("image", "imageB").remove_columns(["label"])
], axis=1)
test_dataset = concatenate_datasets([
    test_examples_A.rename_column("image", "imageA").remove_columns(["label"]),
    test_examples_B.rename_column("image", "imageB").remove_columns(["label"])
], axis=1)

datasets = DatasetDict({
    "train": train_dataset,
    "test": test_dataset
})

```

You might need to make sure that train\_examples\_A and train\_examples\_B have the same length before concatenating them
