Skip to main content

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
Data the column names contains.
Returns:
df_with_cols - A new DataFrame with the columns 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
IndexIDSpeciesColorWeightAgeIs_CatOwner_CommentNew_column_1New_column_2
0dog_001dogblack405False There are no bad dogs, only bad owners.thisthis
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!isis
2cat_002catblack159True****All you need is love and a cat.****aanother
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.newnew
4dog_003dogblack250.5FalseBe the person your dog thinks you are.columncolumn
5ham_001hamsterblack13FalseNo, thank you!II
6ham_002hamstergolden0.250.2FalseNo, thank you!assignedassigned
7cat_003catblack100TrueNo, thank you!..