In data Frames, Empty columns are defined and represented with NaN Value(Not a Number value or undefined or unrepresentable value). There are various methods to add Empty Column to Pandas Dataframe.
Method 1: Using the Assignment Operator.
This method is used to forcefully assign any column a null or NaN value.
In the above example, we are using the assignment operator to assign empty string and Null value to two newly created columns as “Gender” and “Department” respectively for pandas data frames (table). Numpy library is used to import NaN value and use its functionality.
Method 2: Using Dataframe.reindex().
This method is used to create new columns in a dataframe and assign value to these columns(if not assigned, null will be assigned automatically).
In the above example, we created a data frame with two columns “First name and “Age” and later used Dataframe.reindex() method to add two new columns “Gender” and ” Roll Number” to the list of columns with NaN values.
Method 3: Using Dataframe.insert().
This method is used to add a new column to a pandas dataframe at any index location we want and assign the appropriate value as per need.
In the above example, we are using the Dataframe.insert() method on pandas data frames (table) to add an empty column “Roll Number”, here we can also insert the column at any index position we want (as here we placed the value at index location 0).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course