Open In App

How to add Empty Column to Dataframe in Pandas?

Last Updated : 16 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In DataFrames, 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 in Python.

Method 1: Add Empty Column to Dataframe using the Assignment Operator

We are using the assignment operator to assign empty strings to two newly created columns as “Gender” and “Department” respectively for Pandas Dataframes.

Python3




# import required libraries
import numpy as np
import pandas as pd
 
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul',
                                          'Ashish',
                                          'Milan'],
                            "Age": [21,22,23]})
# show the dataframe
print("\n\n---Original Dataframe---\n",
      Mydataframe)
 
# add an empty columns
Mydataframe['Gender'] = ''
Mydataframe['Department'] = ''
 
# show the dataframe
print("---Updated Dataframe---\n",
      Mydataframe)


 Output:

 

Method 2: Add Empty Column to Dataframe using the np.nan 

We are using np.nan values to two newly created columns as “Gender” and “Department” respectively for Pandas Dataframes(table). Numpy library is used to import NaN value and use its functionality.

Python3




# import required libraries
import numpy as np
import pandas as pd
 
# show the dataframe
print("\n\n---Original Dataframe---\n",
      Mydataframe)
 
# add an empty columns
Mydataframe['Gender'] = np.nan
Mydataframe['Department'] = np.nan
 
# show the dataframe
print("---Updated Dataframe---\n",
      Mydataframe)


Output:

 

Method 3: Add Empty Column to Dataframe using the None 

We are using None values to two newly created columns as “Gender” and “Department” respectively for Pandas Dataframes.

Python3




# import required libraries
import numpy as np
import pandas as pd
 
# show the dataframe
print("\n\n---Original Dataframe---\n",
      Mydataframe)
 
# add an empty columns
Mydataframe['Gender'] = None
Mydataframe['Department'] = None
 
# show the dataframe
print("---Updated Dataframe---\n",
      Mydataframe)


Output:

 

Method 4: Add Empty Column to Dataframe using Dataframe.reindex().

We created a Dataframe 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.

Python3




# import pandas library
import pandas as pd
 
# create a dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Preetika',
                                          'Tanya',
                                          'Akshita'],
                            "Age": [25,21,22]})
# show the dataframe
print("---Original Dataframe---\n",
      Mydataframe)
 
# add an empty columns
Mydataframe = Mydataframe.reindex(columns = Mydataframe.columns.tolist()
                                  + ['Gender','Roll Number'])
 
# show the dataframe
print("\n\n---Updated Dataframe---\n",
      Mydataframe)


Output:

 

Method 5: Add Empty Column to Dataframe using Dataframe.insert()

We are using the Dataframe.insert() method on pandas Dataframes 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).

Python3




# import pandas library
import pandas as pd
 
# create a dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Rohan',
                                          'Martin',
                                          'Mary'],
                            "Age": [28,39,21]})
# show the dataframe
print("---Original Dataframe---\n",
      Mydataframe)
 
# add an empty column
Mydataframe.insert(0,'Roll Number','')
 
# show the dataframe
print("\n\n---Updated Dataframe---\n",
      Mydataframe)


Output:

 



Similar Reads

Get column index from column name of a given Pandas DataFrame
In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together. Syntax: DataFrame.columns Return: column names index Syntax: Index.get_loc(key, method=None, tolerance=None) Return: loc : int if unique index, slice if monotonic index, els
2 min read
Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
This article demonstrates multiple examples to convert the Numpy arrays into Pandas Dataframe and to specify the index column and column headers for the data frame. Example 1: In this example, the Pandas dataframe will be generated and proper names of index column and column headers are mentioned in the function. This approach can be used when ther
4 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to add column sum as new column in PySpark dataframe ?
In this article, we are going to see how to perform the addition of New columns in Pyspark dataframe by various methods. It means that we want to create a new column that will contain the sum of all values present in the given row. Now let's discuss the various methods how we add sum as new columns But first, let's create Dataframe for Demonstratio
4 min read
Add Column to Pandas DataFrame with a Default Value
The three ways to add a column to Pandas DataFrame with Default Value. Using pandas.DataFrame.assign(**kwargs)Using [] operatorUsing pandas.DataFrame.insert()Using Pandas.DataFrame.assign(**kwargs) It Assigns new columns to a DataFrame and returns a new object with all existing columns to new ones. Existing columns that are re-assigned will be over
2 min read
Add column with constant value to pandas dataframe
Prerequisite: Pandas In this article, we will learn how to add a new column with constant value to a Pandas DataFrame. Before that one must be familiar with the following concepts: Pandas DataFrame : Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular arrangement with labeled axes (rows and columns). A Data frame may
2 min read
Add column names to dataframe in Pandas
In this article, we are going to see how to add column names to a dataframe. Let us how to add names to DataFrame columns in Pandas. Add Column Names to Pandas DataframeBelow are the steps and methods by which we can add column names in the Pandas dataframe in Python: Creating the DataFrameLet's first create an example DataFrame for demonstration r
2 min read
How to add column from another DataFrame in Pandas ?
In this discussion, we will explore the process of adding a column from another data frame in Pandas. Pandas is a powerful data manipulation library for Python, offering versatile tools for handling and analyzing structured data. Add column from another DataFrame in Pandas There are various ways to add a column from another DataFrame in Pandas. Her
6 min read
Fill empty column - Pandas
Sometimes the dataframe contains an empty column and may pose a real problem in the real life scenario. Missing Data can also refer to as NA(Not Available) values in pandas. In DataFrame sometimes many datasets simply arrive with missing data, either because it exists and was not collected or it never existed. In this article, let's see how to fill
2 min read
Python | Pandas DataFrame.empty
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas. Pandas DataFrame.empty attribute che
2 min read
Practice Tags :