You can add an empty column to a Pandas DataFrame using the following code:
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
# Add an empty column named 'C'
df['C'] = None
In this code, we first create a DataFrame df
with two columns ‘A’ and ‘B’. To add an empty column named ‘C’, we simply assign None
to it. Alternatively, you could assign an empty Series to it, like this:
df['C'] = pd.Series(dtype='float64')
This will create an empty column with data type float. You can specify the data type of the column by changing the dtype
parameter to the appropriate data type.