How to add one row in an existing Pandas DataFrame?
Last Updated :
25 Jun, 2024
Adding rows to a Pandas DataFrame is a common task in data manipulation. Here are the primary methods to add rows to a DataFrame, along with their syntax and examples in Pandas.
Add Row to Pandas Dataframe
# Append the new row to the DataFrame
df = df.append(new_row, ignore_index=True)
print(df)
Now let’s see with the help of examples how we can do this
Add Row to Dataframe using df.loc
We can add a single row using DataFrame.loc. We can append row to dataframe at the last. We can get the number of rows using len(DataFrame.index) for determining the position at which we need to add the new row.
Python
from IPython.display import display, HTML
import pandas as pd
from numpy.random import randint
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}
df = pd.DataFrame(dict)
display(df)
df.loc[len(df.index)] = ['Amy', 89, 93]
display(df)
Output:

Pandas Add Row to Dataframe using df.append
We can also add a new row using the DataFrame.append() function
Python
from IPython.display import display, HTML
import pandas as pd
import numpy as np
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}
df = pd.DataFrame(dict)
display(df)
df2 = {'Name': 'Amy', 'Maths': 89, 'Science': 93}
df = df._append(df2, ignore_index = True)
display(df)
# This code is modified by Susobhan Akhuli
Output:

Python Pandas Add Row to Dataframe using Concat
We can also add multiple rows using the pandas.concat() by creating a new dataframe of all the rows that we need to add and then appending this dataframe to the original dataframe.
Python
from IPython.display import display, HTML
import pandas as pd
import numpy as np
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}
df1 = pd.DataFrame(dict)
display(df1)
dict = {'Name':['Amy', 'Maddy'],
'Maths':[89, 90],
'Science':[93, 81]
}
df2 = pd.DataFrame(dict)
display(df2)
df3 = pd.concat([df1, df2], ignore_index = True)
df3.reset_index()
display(df3)
Output:

How to Add Rows to Pandas Dataframe – FAQs
How to add one row of a DataFrame to another DataFrame?
You can use the append() method in Pandas to add one row from one DataFrame to another DataFrame.
import pandas as pd
# Existing DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5], 'B': [6]}) # Single-row DataFrame to add
# Append row from df2 to df1
df_combined = df1.append(df2, ignore_index=True)
print(df_combined)
How to create a single-row DataFrame in Pandas?
You can create a single-row DataFrame by passing a dictionary with column names as keys and values as a list containing single row data.
import pandas as pd
data = {'A': [1], 'B': [2]} # Single-row data
df = pd.DataFrame(data)
print(df)
How to get 1 row as a DataFrame in Pandas?
If you have an existing DataFrame and want to extract a single row as another DataFrame, you can use slicing (‘iloc[]’) or filtering.
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Get the first row as a DataFrame
row_df = df.iloc[[0]]
print(row_df)
What is the append() method in Pandas?
The ‘append()’ method in Pandas is used to concatenate rows from one DataFrame to another. It creates a new DataFrame by combining rows from the original DataFrame and another DataFrame or Series.
What is the difference between concat and append in DataFrame?
concat() and append() are both methods used to concatenate DataFrames in Pandas:
concat() can concatenate multiple DataFrames along either axis (rows or columns) and allows for more flexible concatenation options.append() is a specific case of concat() used to append rows from one DataFrame to another DataFrame and is more straightforward for appending rows.
import pandas as pd
# Example DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
# Using append()
appended_df = df1.append(df2, ignore_index=True)
# Using concat()
concatenated_df = pd.concat([df1, df2], ignore_index=True)
print("Appended DataFrame:")
print(appended_df)
print("\nConcatenated DataFrame:")
print(concatenated_df)
Output:
Appended DataFrame:
A B
0 1 3
1 2 4
2 5 7
3 6 8
Concatenated DataFrame:
A B
0 1 3
1 2 4
2 5 7
3 6 8
Please Login to comment...