# How to add a new column to a dataset

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%3E%5C
**Category:** 🤗Datasets\
**Created:** [May 29, 2021, 1:06am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453 "2021-05-29T01:06:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chetnakhanna16](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@chetnakhanna16](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchetnakhanna16)%5C
**Post date:** [May 29, 2021, 1:06am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F1 "2021-05-29T01:06:43Z")

</div>

How can I add a new column to my dataset?

I am working on **Cosmos QA dataset** and need to add a new column of the following format:  
**Value(dtype=‘string’, id=None)**

The current dataset has the following features:  
Dataset({  
features: [‘id’, ‘context’, ‘question’, ‘answer0’, ‘answer1’, ‘answer2’, ‘answer3’, ‘label’],  
num\_rows: 25262  
})

Thank you in advance!

---

<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 3, 2021, 11:47am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F2 "2021-06-03T11:47:29Z")

</div>

Hi ! You can use the `add_column` method:

```python
from datasets import load_dataset

ds = load_dataset("cosmos_qa", split="train")

new_column = ["foo"] * len(ds)
ds = ds.add_column("new_column", new_column)

```

and you get a dataset

```auto
Dataset({
    features: ['id', 'context', 'question', 'answer0', 'answer1', 'answer2', 'answer3', 'label', 'new_column'],
    num_rows: 25262
})

```

---

<div class="post-metadata">

**Author:** ![chetnakhanna16](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@chetnakhanna16](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchetnakhanna16)%5C
**Post date:** [June 6, 2021, 7:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F3 "2021-06-06T19:03:39Z")

</div>

Thank you for your reply!

Unfortunately, when I ran the above code, I got the following error:  
**AttributeError: ‘Dataset’ object has no attribute 'add\_column’**

Just FYI, Datasets version = 1.1.2

Thank you!

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [June 7, 2021, 11:51am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F4 "2021-06-07T11:51:43Z")

</div>

You need to update your datasets library to the latest version for this.

---

<div class="post-metadata">

**Author:** ![mjc00](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mjc00](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmjc00)%5C
**Post date:** [March 16, 2022, 9:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F5 "2022-03-16T21:16:55Z")

</div>

> [@sgugger](#):
>
> datasets library

Can you provide more details on this? I am running into the same problem as @ [chetnakhanna16](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchetnakhanna16)

---

<div class="post-metadata">

**Author:** ![marshmellow77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/marshmellow77/32/3279_2.png) [@marshmellow77](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarshmellow77)%5C
**Post date:** [March 16, 2022, 9:24pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F6 "2022-03-16T21:24:00Z")

</div>

On your machine you need to run the command `pip install datasets --upgrade` to update your dataset library to the latest version.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![mjc00](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mjc00](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmjc00)%5C
**Post date:** [March 16, 2022, 9:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F7 "2022-03-16T21:41:44Z")

</div>

Here is my current dataset version  
`datasets==2.0.0`  
Which is the same after running the suggested command. Still the problem persists.

In essence I am trying to find a way to add the `input_ids` and `attention_mask` back into my dataset.

The reason why I am doing this, is that when running this function:

```auto
model_ckpt = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
def tokenize(batch):
    return tokenizer(batch["text"], 
                     padding=True, 
                     truncation=True)

```

on a sample of my dataset → `print(tokenize(clean_dataset["train"][:2]))` I get the following error  
`ValueError: text input must of type `str`(single example),`List[str]`(batch or single pretokenized example) or`List[List[str]]` (batch of pretokenized examples).`

However, when I run this `complete_tok = tokenizer(list(x_complete), truncation=True, padding=True)` where `x_complete` is an np array the tokenizer seems to run fine and creates `input_ids` and `attention_mask`

---

<div class="post-metadata">

**Author:** ![marshmellow77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/marshmellow77/32/3279_2.png) [@marshmellow77](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarshmellow77)%5C
**Post date:** [March 16, 2022, 9:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F8 "2022-03-16T21:52:26Z")

</div>

Can you verify that `clean_dataset["train"][:2]` is indeed of type `str`?

---

<div class="post-metadata">

**Author:** ![mjc00](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mjc00](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmjc00)%5C
**Post date:** [March 16, 2022, 10:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F9 "2022-03-16T22:03:05Z")

</div>

Found my mistake → needed to cast the clean\_Description batch values to a list, code runs now. Thank you 🥸 🤓

```auto
def tokenize(batch):
    return tokenizer(list(batch["clean_Description"].values), 
                     padding=True, 
                     truncation=True)

```

---

<div class="post-metadata">

**Author:** ![crossdelenna](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@crossdelenna](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fcrossdelenna)%5C
**Post date:** [July 26, 2022, 4:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F10 "2022-07-26T04:31:11Z")

</div>

Hi guys, I am new here, just started using 🤗 Those like me who are facing the same issue, I think the error is because of dataset type is ‘dict’ and that’s why it gives **AttributeError: ‘Dataset’ object has no attribute 'add\_column’**

My dataset structure is Dataset({  
train: [‘index’, ‘text’, ‘file’],  
num\_rows: 5000  
})

So this is how I solved it

> my\_data[“train”] = my\_data[“train”].add\_column(“audio”,temp\_data[‘audio’])

Hope I am not doing anything wrong.

---

<div class="post-metadata">

**Author:** ![DannyAI](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/dannyai/32/18432_2.png) [@DannyAI](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FDannyAI)%5C
**Post date:** [August 10, 2023, 12:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F12 "2023-08-10T12:12:02Z")

</div>

> [@crossdelenna](#):
>
> temp\_data[‘audio’]

Should be a list, you could convert the temp\_data[“audio”] to a list using

> temp\_data["audio].to\_list()

---

<div class="post-metadata">

**Author:** ![hfreedma](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hfreedma](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhfreedma)%5C
**Post date:** [October 3, 2023, 2:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-add-a-new-column-to-a-dataset%2F6453%2F13 "2023-10-03T14:21:29Z")

</div>

I had trouble with the `add_column` method, maybe it has been deprecated since this post?  
However it is possible to create a Dataset directly from a Python dictionary using the `Dataset.from_dict` method. Using this it is possible to add a column to a Dataset by extracting existing Dataset columns into a Python dictionary, updating the dictionary with the desired column, then re-creating the Dataset object with the additional column(s).
