Pandas How To Uncategorized How to add empty column in Pandas

How to add empty column in Pandas

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post