4 ways to add empty column to pandas DataFrame


Written By - Sravan Kumar
Advertisement

Add empty column/s to pandas DataFrame

We can add empty columns to the DataFrame by using the following methods.

  1. Using Assignment operator with blank/empty value
  2. Using Dataframe.reindex() with NaN
  3. Using Dataframe.insert() with blank/empty value
  4. Using Assignment operator with NaN
  5. Using Dataframe.insert() with NaN

 

Method 1 : Using Assignment operator with blank/empty value

We can add an empty column to the pandas DataFrame with assignment operator and set the values in this empty columns as empty or blank.

Syntax:

dataframe['column']=''

where,

  1. dataframe is the input dataframe
  2. column is the name of the empty column
  3. ' ' represents empty string

 

Example : Add single empty column to existing DataFrame

In this example, we are adding 'market name' column with empty values.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
dataframe['market name'] = ''

# display the dataframe
print(dataframe)

Output:

            id            name    cost  quantity market name
item-1  foo-23  ground-nut oil  567.00         1            
item-2  foo-13         almonds  562.56         2            
item-3  foo-02           flour   67.00         3            
item-4  foo-31         cereals   76.09         2

 

Example : Add multiple empty columns to existing DataFrame

Add market name and address column in the dataframe with blank data.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
dataframe['market name'] = ''

# add column named 'address' with blank
dataframe['address'] = ''

# display the dataframe
print(dataframe)

Output:

Advertisement
            id            name    cost  quantity market name address
item-1  foo-23  ground-nut oil  567.00         1                    
item-2  foo-13         almonds  562.56         2                    
item-3  foo-02           flour   67.00         3                    
item-4  foo-31         cereals   76.09         2

 

Method 2 : Using Dataframe.reindex() with NaN

We can add an empty column to the pandas DataFrame with tolist() and set the values in this empty columns as NaN through reindex() method.

Syntax:

dataframe.reindex(columns=dataframe.columns.tolist() + ['column'])
  1. dataframe is the input dataframe
  2. column is the name of the empty column
  3. columns is the function to get columns and convert into list using tolist() method.

 

Example : Add single empty column to existing DataFrame with NaN value

In this example, we are adding 'market name' column with NaN values.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
# display the dataframe
print(dataframe.reindex(columns=dataframe.columns.tolist() + ['market name']))

Output:

            id            name    cost  quantity  market name
item-1  foo-23  ground-nut oil  567.00         1          NaN
item-2  foo-13         almonds  562.56         2          NaN
item-3  foo-02           flour   67.00         3          NaN
item-4  foo-31         cereals   76.09         2          NaN

 

Example : Add multiple empty columns to existing DataFrame with NaN value

Add market name and address column in the dataframe with NaN values.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
# add column named 'address' with blank
# display the dataframe
print(dataframe.reindex(columns=dataframe.columns.tolist() + ['market name','address']))

Output:

            id            name    cost  quantity  market name  address
item-1  foo-23  ground-nut oil  567.00         1          NaN      NaN
item-2  foo-13         almonds  562.56         2          NaN      NaN
item-3  foo-02           flour   67.00         3          NaN      NaN
item-4  foo-31         cereals   76.09         2          NaN      NaN

 

Method 3 : Using Dataframe.insert() with blank/empty value

We can add an empty column to the pandas DataFrame with insert() method

Syntax:

Advertisement
dataframe.insert(position,column,value)
  1. dataframe is the input dataframe
  2. position is the index value that represents the column to be inserted at the given position
  3. value is the default value assigned to the column, Here it is blank - ' '.

 

Example : Insert empty column in at custom position of existing DataFrame

In this example, we are adding 'market name' column with empty/blank values at first position. Replace 0 in dataframe.insert(0,'market name','') with the column number to add the empty column.

# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
dataframe.insert(0,'market name','')

# display the dataframe
print(dataframe)

Output:

       market name      id            name    cost  quantity
item-1              foo-23  ground-nut oil  567.00         1
item-2              foo-13         almonds  562.56         2
item-3              foo-02           flour   67.00         3
item-4              foo-31         cereals   76.09         2

 

Method 4 : Using Assignment operator with NaN

We can add an empty column to the pandas DataFrame with assignment operator and set the values in this empty columns as NaN.

Syntax:

dataframe['column']=numpy.nan

where,

  1. dataframe is the input dataframe
  2. column is the name of the empty column
  3. numpy.nan represents NaN value

 

Example : Add single empty column to existing DataFrame with NaN value

Example :In this example, we are adding 'market name' column with NaN values.

Advertisement
# import the module
import pandas

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with blank
dataframe['market name'] = ''

# display the dataframe
print(dataframe)

Output:

            id            name    cost  quantity  market name
item-1  foo-23  ground-nut oil  567.00         1          NaN
item-2  foo-13         almonds  562.56         2          NaN
item-3  foo-02           flour   67.00         3          NaN
item-4  foo-31         cereals   76.09         2          NaN

 

Example : Add multiple empty columns to existing DataFrame with NaN value

Add market name and address column in the dataframe with  NaN.

# import the module
import pandas
import numpy

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with NaN
dataframe['market name'] = numpy.nan

# add column named 'address' with NaN
dataframe['address'] = numpy.nan

# display the dataframe
print(dataframe)

Output:

            id            name    cost  quantity  market name  address
item-1  foo-23  ground-nut oil  567.00         1          NaN      NaN
item-2  foo-13         almonds  562.56         2          NaN      NaN
item-3  foo-02           flour   67.00         3          NaN      NaN
item-4  foo-31         cereals   76.09         2          NaN      NaN

 

Method 5 : Using Dataframe.insert() with NaN

We can add an empty column to the pandas DataFrame with insert() method with NaN values

Syntax:

dataframe.insert(position,column,value)
  1. dataframe is the input dataframe
  2. position is the index value that represents the column to be inserted at the given position
  3. value is the default value assigned to the column, Here it is NaN.

 

Example : Specify the position to add empty column into existing DataFrame

In this example, we are adding 'market name' column with NaN values at last position. Replace 4 in dataframe.insert(4,'market name',numpy.nan) with the column number where you intend to add the empty column:

# import the module
import pandas
import numpy

# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':[1,2,3,2]}

# pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

# add column named 'market name' with NaN
dataframe.insert(4,'market name',numpy.nan)

# display the dataframe
print(dataframe)

Output:

            id            name    cost  quantity  market name
item-1  foo-23  ground-nut oil  567.00         1          NaN       
item-2  foo-13         almonds  562.56         2          NaN       
item-3  foo-02           flour   67.00         3          NaN       
item-4  foo-31         cereals   76.09         2          NaN

 

Summary

In this tutorial , we discussed how to add an empty column in pandas DataFrame. By using index(), insert() and assignment operators, we added empty column by assigning empty/blank and NaN values. We came to the point that , by using insert() method , we can add a column at any of the position through position parameter. reindex() function not only add a column it will reassign the index values in the entire dataframe.

 

References

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment