df.assign()
df.assign(name_of_column=column_data)
Adds/replaces a column. name_of_column
should not have quotes or spaces.
- Input:
- name_of_column : string
- Name of column to assign list to.
- column_data : list or Series
- The data
name_of_column
will contain. - Returns:
- A new DataFrame with
name_of_column
in addition to all the existing columns. - Return Type:
- DataFrame
pets = pets.assign(New_column_1=['this', 'is', 'a', 'new', 'column', 'I', 'assigned', '.'],
New_column_2=['this', 'is', 'another', 'new', 'column', 'I', 'assigned', '.'])
pets
Index | ID | Species | Color | Weight | Age | Is_Cat | Owner_Comment | New_column_1 | New_column_2 |
---|---|---|---|---|---|---|---|---|---|
0 | dog_001 | dog | black | 40 | 5 | False | There are no bad dogs, only bad owners. | this | this |
1 | cat_001 | cat | golden | 1.5 | 0.2 | True | My best birthday present ever!!! | is | is |
2 | cat_002 | cat | black | 15 | 9 | True | ****All you need is love and a cat.**** | a | another |
3 | dog_002 | dog | white | 80 | 2 | False | Love is a wet nose and a wagging tail. | new | new |
4 | dog_003 | dog | black | 25 | 0.5 | False | Be the person your dog thinks you are. | column | column |
5 | ham_001 | hamster | black | 1 | 3 | False | No, thank you! | I | I |
6 | ham_002 | hamster | golden | 0.25 | 0.2 | False | No, thank you! | assigned | assigned |
7 | cat_003 | cat | black | 10 | 0 | True | No, thank you! | . | . |