Different methods to add row to existing DataFrame in pandas
In this tutorial we will discuss how to add row to existing pandas DataFrame using the following methods:
- Using
loc[]
function with index - Using
append()
function - Using
panads.concat()
function byignoring index
- Using
panads.concat()
function withindex
.
Create pandas DataFrame with example data
DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.
We can create the DataFrame by using pandas.DataFrame() method.
Syntax:
pandas.DataFrame(input_data,columns,index)
Parameters:
It will take mainly three parameters
input_data
is represents a list of datacolumns
represent the columns names for the dataindex
represent the row numbers/values
We can also create a DataFrame using dictionary by skipping columns and indices.
Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.
# 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'])
# display the dataframe
print(dataframe)
Output:
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 1 : Using loc[] function with index
Here , we are going to add a row by using loc[]
function through index function. We need to specify the index position by len()
function to insert a row at last position of the dataframe and assign the values of a row using list.
Syntax:
dataframe.loc[len(dataframe.index)] =[list of values]
where,
- dataframe is the input dataframe
- list of values represents the row
Example:
Python program to add a row at last with the list of values - ['foo-45', 'wheat', 93,1]
# 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 row
dataframe.loc[len(dataframe.index)] = ['foo-45', 'wheat', 93,1]
# display the final dataframe
print(dataframe)
Output:
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
4 foo-45 wheat 93.00 1
Method 2 : Using append() function
Here we are using append()
function to add a row to an existing dataframe by ignoring the index. We are going to append a row which is created from a dictionary and exclude the index using ignore_index
parameter. It is set to True
.
Syntax:
dataframe.append(row, ignore_index = True)
where,
- dataframe is the input dataframe
- row specifies a dictionary of values
Example:
Python program to add a row by ignoring the index using append() function.
# 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'])
# define a row
final_row= {'id':'foo-45','name': 'wheat', 'cost':93,'quantity':1}
#append final_row
dataframe = dataframe.append(final_row, ignore_index = True)
# display the final dataframe
print(dataframe)
Output:
id name cost quantity
0 foo-23 ground-nut oil 567.00 1
1 foo-13 almonds 562.56 2
2 foo-02 flour 67.00 3
3 foo-31 cereals 76.09 2
4 foo-45 wheat 93.00 1
If we want to display the index, then we no need to specify the ignore_index
parameter.
Example: Python program to display index by adding row
# 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'])
# define a row
final_row= {'id':'foo-45','name': 'wheat', 'cost':93,'quantity':1}
#append final_row
dataframe = pandas.concat([dataframe,pandas.DataFrame((final_row),index=['item-5'])])
# display the final dataframe
print(dataframe)
Output:
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
item-5 foo-45 wheat 93.00 1
Method 3 : Using panads.concat() function by ignoring index
Here we are using pandas.concat()
function to add a row to an existing dataframe by ignoring the index. We are going to append a row which is created from a dictionary and exclude the index using ignore_index
parameter. It is set to True
.
Syntax:
pandas.concat([dataframe,pandas.DataFrame((row),index)], ignore_index = True)
where,
- dataframe is the input dataframe
- row specifies a dictionary of values
- index specifies row index
Example: Python program to add a row by ignoring the index using append()
function.
# 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'])
# define a row
final_row= {'id':'foo-45','name': 'wheat', 'cost':93,'quantity':1}
#append final_row
dataframe = pandas.concat([dataframe,pandas.DataFrame((final_row),index=['item-5'])], ignore_index = True)
# display the final dataframe
print(dataframe)
Output:
id name cost quantity
0 foo-23 ground-nut oil 567.00 1
1 foo-13 almonds 562.56 2
2 foo-02 flour 67.00 3
3 foo-31 cereals 76.09 2
4 foo-45 wheat 93.00 1
We can also add multiple rows at a time.
Example: In this example we are going to add 2 rows
# 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'])
# define a row
final_row= {'id':['foo-45','foo-25'],'name': ['wheat','ragi-powder'], 'cost':[89,93],'quantity':[3,1]}
#append final_row
dataframe = pandas.concat([dataframe,pandas.DataFrame((final_row),index=['item-5','item-6'])], ignore_index = True)
# display the final dataframe
print(dataframe)
Output:
id name cost quantity
0 foo-23 ground-nut oil 567.00 1
1 foo-13 almonds 562.56 2
2 foo-02 flour 67.00 3
3 foo-31 cereals 76.09 2
4 foo-45 wheat 89.00 3
5 foo-25 ragi-powder 93.00 1
Method 4 : Using panads.concat() function with index
Here we are using pandas.concat()
function to add a row to an existing dataframe by including the index. We are going to append a row which is created from a dictionary and include the index using ignore_index
parameter. It is set to False
.
Syntax:
pandas.concat([dataframe,pandas.DataFrame((row),index)], ignore_index = False)
where,
- dataframe is the input dataframe
- row specifies a dictionary of values
- index specifies row index
Example: Python program to add a row by including the index using append()
function.
# 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'])
# define a row
final_row= {'id':'foo-45','name': 'wheat', 'cost':93,'quantity':1}
#append final_row
dataframe = pandas.concat([dataframe,pandas.DataFrame((final_row),index=['item-5'])], ignore_index = False)
# display the final dataframe
print(dataframe)
Output:
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
item-5 foo-45 wheat 93.00 1
We can also add multiple rows at a time.
Example: In this example we are going to add 2 rows
# 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'])
# define a row
final_row= {'id':['foo-45','foo-25'],'name': ['wheat','ragi-powder'], 'cost':[89,93],'quantity':[3,1]}
#append final_row
dataframe = pandas.concat([dataframe,pandas.DataFrame((final_row),index=['item-5','item-6'])], ignore_index = False)
# display the final dataframe
print(dataframe)
Output:
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
item-5 foo-45 wheat 89.00 3
item-6 foo-25 ragi-powder 93.00 1
Summary
In this tutorial we discussed how to add row/s to an existing dataframe using append(), loc[] and concat()
functions. We also included and excluded indices with concat()
function. We understand that we can only add multiple rows for an existing dataframe using concat()
function , but not with append()
function.
References
- Pandas - append()
- Pandas - concat()