How to add an empty column to a dataframe?

In pandas, you can add an empty column to a DataFrame using the assign() method or the insert() method.

Here is an example using the assign() method:

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# add an empty column named 'C'
df = df.assign(C=None)

print(df)

This will add a column named 'C' with all values as None to the dataframe.

Watch a course Python - The Practical Guide

You can also use the insert() method to add an empty column to a specific location in the DataFrame:

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# add an empty column named 'C' at index 1
df.insert(1, 'C', None)

print(df)

This will add a column named 'C' with all values as None to the dataframe at index 1.