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
In [1]:
import pandas as pd
In [2]:
# Create empty dataframe
df = pd.DataFrame()
# Create a column
df['name'] = ['John', 'Steve', 'Sarah']
# View dataframe
df
Out[2]:
In [3]:
# Assign a new column to df called 'age' with a list of ages
df.assign(age = [31, 32, 19])
Out[3]: