Add a Row to a Pandas DataFrame
You can add a row to a Pandas DataFrame using pd.concat() or by assigning values with DataFrame.loc. The older DataFrame.append() method was removed in pandas 2.0, so new programs should not use it.
This tutorial shows how to add a dictionary, a Pandas Series, and multiple rows to a DataFrame. It also explains how to preserve or create row indexes and how pandas handles missing or additional columns.
Add a Dictionary Row with pd.concat()
To add a dictionary as a new row, first convert the dictionary to a one-row DataFrame. Enclose the dictionary in a list so that pandas interprets it as one record. Then combine it with the original DataFrame using pd.concat().
Syntax for Adding One Dictionary Row
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
The ignore_index=True argument creates a consecutive index for the combined DataFrame. Because pd.concat() returns a new DataFrame, assign the result back to df or another variable.
Python Example
import pandas as pd
# Initialize a DataFrame
df = pd.DataFrame({
'a': [14, 52, 46],
'b': [32, 85, 64],
'c': [88, 47, 36]
})
# Define the new row as a dictionary
new_row = {'a': 11, 'b': 22, 'c': 33}
# Convert the dictionary to a one-row DataFrame and concatenate it
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
print(df)
Output
a b c
0 14 32 88
1 52 85 47
2 46 64 36
3 11 22 33
The dictionary keys match the DataFrame column names, so each value is placed in its corresponding column.
Add a Row In Place with DataFrame.loc
When you need to add a single row, loc provides a direct alternative. Assign the new values to an unused index label. For a DataFrame with a default consecutive index, len(df) is commonly used as the next index.
import pandas as pd
df = pd.DataFrame({
'a': [14, 52, 46],
'b': [32, 85, 64],
'c': [88, 47, 36]
})
# Add one row at the next integer index
df.loc[len(df)] = {'a': 11, 'b': 22, 'c': 33}
print(df)
Output
a b c
0 14 32 88
1 52 85 47
2 46 64 36
3 11 22 33
Unlike pd.concat(), assignment through loc changes the existing DataFrame. Use this approach only when the selected index label does not already identify a row that must be preserved. Assigning to an existing label replaces that row’s values.
Add a Pandas Series as a New Row
A Series can be converted to a one-row DataFrame by calling to_frame().T. The transpose operation changes the Series from a column into a row before concatenation.
import pandas as pd
df = pd.DataFrame({
'a': [14, 52, 46],
'b': [32, 85, 64],
'c': [88, 47, 36]
})
new_row = pd.Series({'a': 11, 'b': 22, 'c': 33})
df = pd.concat([df, new_row.to_frame().T], ignore_index=True)
print(df)
Output
a b c
0 14 32 88
1 52 85 47
2 46 64 36
3 11 22 33
The Series index labels become the column names of the new row. They should therefore correspond to the columns in the existing DataFrame.
Add Multiple Rows to a Pandas DataFrame
For multiple records, create one DataFrame from a list of dictionaries and concatenate it once. This is preferable to repeatedly adding rows inside a loop because each concatenation may require pandas to allocate and copy data.
import pandas as pd
df = pd.DataFrame({
'a': [14, 52, 46],
'b': [32, 85, 64],
'c': [88, 47, 36]
})
new_rows = [
{'a': 11, 'b': 22, 'c': 33},
{'a': 24, 'b': 35, 'c': 46}
]
df = pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)
print(df)
Output
a b c
0 14 32 88
1 52 85 47
2 46 64 36
3 11 22 33
4 24 35 46
Preserve Custom Index Labels While Adding Rows
Omit ignore_index=True when the existing and new index labels need to be retained. Set the index of the new one-row DataFrame explicitly before concatenating it.
import pandas as pd
df = pd.DataFrame(
{'name': ['Asha', 'Ravi'], 'score': [82, 76]},
index=['student-1', 'student-2']
)
new_row = pd.DataFrame(
[{'name': 'Meera', 'score': 91}],
index=['student-3']
)
df = pd.concat([df, new_row])
print(df)
Output
name score
student-1 Asha 82
student-2 Ravi 76
student-3 Meera 91
Handle Missing or Additional Columns in the New Row
pd.concat() aligns values by column name. A missing key produces a missing value in that column. A key that does not exist in the original DataFrame creates an additional column, with missing values for earlier rows.
import pandas as pd
df = pd.DataFrame({
'name': ['Asha', 'Ravi'],
'score': [82, 76]
})
# The score value is missing, and grade is a new column
new_row = {'name': 'Meera', 'grade': 'A'}
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
print(df)
Output
name score grade
0 Asha 82.0 NaN
1 Ravi 76.0 NaN
2 Meera NaN A
Validate the dictionary keys before concatenation when the resulting DataFrame must retain an exact schema.
Legacy DataFrame.append() Examples
The following examples use the former DataFrame.append() API. They are retained to explain code written for older pandas versions. In pandas 2.0 and later, these examples raise an AttributeError. Use the equivalent pd.concat() or loc examples above for current code.
Syntax
The syntax of DataFrame.append() method is
df = df.append(new_row, ignore_index=True)
where df is the DataFrame and new_row is the row appended to DataFrame.
append() returns a new DataFrame with the new row added to original dataframe. Original DataFrame is not modified by append() method.
Add Row (Python Dictionary) to Pandas DataFrame
In the following Python example, we will initialize a DataFrame and then add a Python Dictionary as row to the DataFrame, using append() method.
The python dictionary should contain the column names as keys and corresponding values as dictionary values.
Python Example
import pandas as pd
#initialize a dataframe
df = pd.DataFrame({
'a':[14, 52, 46],
'b':[32, 85, 64],
'c':[88, 47, 36]})
#new row as dictionary
row1 = {'a':11, 'b':22, 'c':33}
#append row to dataframe
df = df.append(row1, ignore_index=True)
#print the dataframe
print(df)
Output
a b c 0 14 32 88 1 52 85 47 2 46 64 36 3 11 22 33
append() method has created a new DataFrame and added the new row to this DataFrame.
Add Row (Pandas Series) to Pandas DataFrame
In the following Python example, we will initialize a DataFrame and then add a Pandas Series as row to the DataFrame, using append() method.
Python Example
import pandas as pd
#initialize a dataframe
df = pd.DataFrame({
'a':[14, 52, 46],
'b':[32, 85, 64],
'c':[88, 47, 36]})
#new row as Pandas Series
row1 = pd.Series(data={'a':11, 'b':22, 'c':33}, name=len(df))
#append row to dataframe
df = df.append(row1)
#print the dataframe
print(df)
Output
a b c 0 14 32 88 1 52 85 47 2 46 64 36 3 11 22 33
While initializing the Series, we assigned len(df) to name. name is translated to index in DataFrame. You can assign any value of your choice to the name based on requirement.
Choosing Between pd.concat() and DataFrame.loc
- Use
pd.concat()when combining an existing DataFrame with another DataFrame or with a collection of new rows. - Use
df.loc[index] = valuesfor a direct single-row assignment when the intended index label is known. - Build a list of records first and concatenate once when rows are produced in a loop.
- Use
ignore_index=Truewhen the combined DataFrame should receive a new consecutive integer index. - Omit
ignore_index=Truewhen meaningful custom index labels must be preserved.
Common Questions About Adding Pandas DataFrame Rows
Why does DataFrame.append() not work?
DataFrame.append() was deprecated and then removed from pandas 2.0. Replace it with pd.concat(), or use DataFrame.loc for a direct single-row assignment.
How do I add a row without changing the existing index?
Create the new row with its intended index label and call pd.concat() without ignore_index=True. pandas will preserve the existing and new labels.
Can I add a row when some column values are missing?
Yes. When a new row does not contain a value for an existing column, pandas fills that position with a missing value such as NaN or None, depending on the column’s data type.
What is the efficient way to add many rows?
Collect the records in a list, construct one DataFrame from the list, and concatenate it with the original DataFrame once. Avoid repeatedly concatenating one row during every loop iteration.
Pandas DataFrame Row Addition Summary
Use pd.concat() to combine a DataFrame with one or more new rows. Use DataFrame.loc when directly assigning a single row to a known index. For current pandas versions, replace older DataFrame.append() code because that method is no longer available.
In this Pandas Tutorial, we learned how to add dictionary, Series, and multiple rows to a Pandas DataFrame while managing indexes and column alignment.
TutorialKart.com