v Assign A New Column To A Pandas DataFrame - Python

Assign A New Column To A Pandas DataFrame

Preliminaries

import pandas as pd

Create Dataframe

# Create empty dataframe
df = pd.DataFrame()

# Create a column
df['name'] = ['John', 'Steve', 'Sarah']

# View dataframe
df
name
0 John
1 Steve
2 Sarah

Assign New Column To Dataframe

# Assign a new column to df called 'age' with a list of ages
df.assign(age = [31, 32, 19])
name age
0 John 31
1 Steve 32
2 Sarah 19