Title: Assign A New Column To A Pandas DataFrame
Slug: pandas_assign_new_column_dataframe
Summary: Assign a new column to a pandas dataframe.
Date: 2017-01-16 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon

Preliminaries


In [1]:
import pandas as pd

Create Dataframe


In [2]:
# Create empty dataframe
df = pd.DataFrame()

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

# View dataframe
df


Out[2]:
name
0 John
1 Steve
2 Sarah

Assign New Column To Dataframe


In [3]:
# Assign a new column to df called 'age' with a list of ages
df.assign(age = [31, 32, 19])


Out[3]:
name age
0 John 31
1 Steve 32
2 Sarah 19