Assign/Add an empty or null column to the dataframe in pandas

In This Section we will be focusing on how to assign a column with empty or null values in pandas dataframe, how to add an empty or null column to the dataframe in pandas python, There are multiple ways to do it, like adding an empty column or null column at the first position and last position (start and end of the dataframe) , adding multiple nan columns to the dataframe in pandas. let’s look at each of these cases in pandas with an example for each.

  • Assign or replace existing column with empty or null values in pandas python
  • Add an empty or null column to the dataframe.
  • Add an empty or null column to the first position of the dataframe in pandas (start of the dataframe)
  • Add multiple null or empty columns to the dataframe in pandas (to end of the dataframe)
  • Add multiple null or empty column to the start of the dataframe in pandas

 

 

Create Dataframe:

 

## create dataframe 

import pandas as pd 
import numpy as np 
#Create a DataFrame 
import pandas as pd 
import numpy as np 
d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'], 
     'Age':[26,23,23,23,23,24], 
     'Score':[85,31,55,55,31,77],
     'City':['Newyork','Seattle','Toronto','California','Delhi','Mumbai']} 
    
df = pd.DataFrame(d,columns=['Name','Age','Score','City']) 
df

The Resultant dataframe is

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-1

 

 

Add new column with null or nan or empty values in pandas:

In the below example we have

  • added the new column with Null value in pandas
  • added the new Blank column in pandas
  • added new column with None value in pandas
 

#### add new column with null or nan or empty values
#### add new column with None value
df["Nick_Name1"] = None 

#### add new column with null value
df["Nick_Name2"] = np.nan

#### add new blank column
df["Nick_Name3"] = ""

df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-2

 

 

Add new column to pandas dataframe with default Empty column:

In the below example we have created a default blank column to the dataframe with lambda function as shown below

 

#### Add blank column using lambda function 

df["Blank_Column"] = df.apply(lambda _: ' ', axis=1)
df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-6

 

 

Add blank column to the start of the dataframe:

In the below example we have added a blank column to the start of the dataframe .i.e. to the 1st position.

 

df.insert(0,"Blank_Column", " ")
df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-7

 

 

Replace an existing column with empty or null values in pandas python:

In the below example we have replaced existing column with empty values in pandas

 

df["City"] = " "
df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-3

 

In the below example we have replaced existing column with null values i.e. NaN values in pandas using np.nan

 

df["City"] = np.nan
df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-4

 

In the below example we have replaced existing column with None values in pandas

 

df["City"] = None
df

so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-5

 

 

 

Add multiple columns with blank, null and nan values in pandas python

In the below example we have used assign function to add multiple blank columns, null  or nan columns and  None  columns with None values in pandas

 

df2 = df.assign(Blank_Column=" ", NaN_Column = np.nan, None_Column=None)
df2

by default the multiple blank columns, multiple null and nan columns are added at the last position of the dataframe, so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-8

 

 

 

Add multiple columns at start of the dataframe in pandas with blank, null and nan values

In the below example we have used assign function to add multiple nan columns with NaN values at start of the pandas dataframe i.e. at the 1st position

 

list_existing_cols=df.columns.tolist()

added_cols=["NaN_Column_1", "NaN_Column_2"]
tot_calls= added_cols +list_existing_cols


df2 = df.reindex(columns=tot_calls)
df2

In short, a dataframe is converted to list and newly added NaN columns are concatenated to the front positions so the resultant dataframe will be

 

Assign-or-Add-an-empty-or-null-column-to-the-dataframe-in-pandas-9

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts