Add row to Pandas DataFrame
In this post, you will learn how to add a row to a dataframe pandas. In the development process, we may face a situation where we need to add a row to an existing dataframe. There are different ways to achieve this; here we have mentioned four of them.
Add row to Pandas DataFrame using append() method
We can use the append() method to append a row to an existing dataframe. This method returns a new dataframe object by appending the rows of another dataframe to the end of the given dataframe. Syntax-
DataFrame.append(object, ignore_index=False, verify_integrity=False, sort=None)
Here, object is a dictionary or Pandas Series, or Dataframe. If ignore_index is true, do not use the index labels. If the verify_integrity is True, raise ValueError on creating an index with duplicates.
# importing module
from pandas import DataFrame
# Creating the first Dataframe
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = DataFrame(fruit_chart,columns=['Fruit','Quantity','Price'])
# Print df
print(df)
# Adding new row
new_dict = {'Fruit': 'Orange', 'Quantity': 30, 'Price': 100}
new_df = df.append(new_dict, ignore_index = True)
# Print new_df
print(new_df)
Output of the above code:
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
5 Orange 30 100
Add row to Pandas DataFrame using concat() method
We can use the concat() function in pandas to append either columns or rows from one DataFrame to another. It has the following syntax -
pd.concat(dataframe, ignore_index, axis)
Here, the dataframe can be either a single dataframe or a list of dataframes that need to be concatenated. If ignore_index is true, do not use the index labels. The axis=0 tells Pandas to stack the second DataFrame under the first one, and the axis=1 will stack the columns in the second DataFrame to the right of the first DataFrame.
# importing module
import pandas as pd
# Creating the first Dataframe
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = pd.DataFrame(fruit_chart,columns=['Fruit','Quantity','Price'])
# Print df
print(df)
# Adding new row
new_dict = pd.DataFrame({'Fruit': ['Watermelon'], 'Quantity': [10], 'Price': [80]})
new_df = pd.concat([df, new_dict], ignore_index = True, axis = 0)
# Print new_df
print(new_df)
Output of the above code:
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
5 Watermelon 10 80
Add row to Pandas DataFrame using iloc[] method
Dataframe provides iloc[] attribute which is purely integer-location based indexing for selection by position. We can use the iloc[] attribute to add a row at a specific position in the dataframe. In the given example, we are directly adding a row at the index position 1. It replaced the values available in that position with the new values.
# importing module
import pandas as pd
# Creating the first Dataframe
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = pd.DataFrame(fruit_chart,columns=['Fruit','Quantity','Price'])
# Print df
print(df)
# Adding new row at the index position 1
df.iloc[1]= ['Papaya', 45, 120]
# Print df
print(df)
Output of the above code:
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
Fruit Quantity Price
0 Apple 12 200
1 Papaya 45 120
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
Add row to Pandas DataFrame using loc[] method
We can add a row to the dataframe using the loc parameter. This attribute is used to access a set of rows from the dataframe using the index label.
# importing module
import pandas as pd
# Creating the first Dataframe
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = pd.DataFrame(fruit_chart,columns=['Fruit','Quantity','Price'], index=['a','b','c','d','e'])
# Print df
print(df)
# Adding new row
df.loc['f']= ['Coconut', 15, 320]
# Print df
print(df)
Output of the above code:
Fruit Quantity Price
a Apple 12 200
b Banana 20 200
c Pear 20 140
d Kiwi 31 300
e Pineapple 10 550
Fruit Quantity Price
a Apple 12 200
b Banana 20 200
c Pear 20 140
d Kiwi 31 300
e Pineapple 10 550
f Coconut 15 320
Related Articles
Python convert dataframe to numpy arrayWrite Python Pandas Dataframe to CSV
Python Pandas DataFrame
Python program to list even and odd numbers of a list
Convert List to Dataframe Python
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Convert dictionary to dataframe Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Python Pandas CSV to Dataframe
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary