How to Add an Empty Column to a Pandas Dataframe?

Spread the love

Problem –

You want to add an empty column to a pandas dataframe.

Solution –

Let’s read a dataset to illustrate.

import pandas as pd
import numpy as np

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/Restaurant.csv'
df = pd.read_csv(url)
df.head()

Now, let’s say I want to add an empty column location to this dataframe. To do that I will just create a new column and fill it with np.nan.

df['location'] = np.nan

Rating: 1 out of 5.

Leave a Reply