Table of Contents
Add empty column/s to pandas DataFrame
We can add empty columns to the DataFrame by using the following methods.
- Using Assignment operator with blank/empty value
- Using
Dataframe.reindex()
with NaN - Using
Dataframe.insert()
with blank/empty value - Using Assignment operator with NaN
- 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,
- dataframe is the input dataframe
- column is the name of the empty column
- ' ' 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:
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'])
- dataframe is the input dataframe
- column is the name of the empty column
- 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:
dataframe.insert(position,column,value)
- dataframe is the input dataframe
- position is the index value that represents the column to be inserted at the given position
- 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,
- dataframe is the input dataframe
- column is the name of the empty column
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.
# 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)
- dataframe is the input dataframe
- position is the index value that represents the column to be inserted at the given position
- 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