Assign A New Column To A Pandas DataFrame

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.


import pandas as pd

Create Dataframe

# Create empty dataframe
df = pd.DataFrame()

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

# View dataframe
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