You have three options if you want to add a row to Pandas DataFrame: the append()
method, loc[]
indexer and the concat()
method. All three methods are similar, but there are a few key differences to know.
Pandas Add Row to a DataFrame Methods
Append()
: This method allows you to add one or more rows to an existing Pandas DataFrame.Loc[]
: This method allows you to access a row, group of rows or columns or a boolean array and add it to Pandas DataFrame.Concat()
: This method allows you to concatenate two Pandas DataFrame objects and add them as a row.
This article will show you how to add one row to Pandas DataFrame. You’ll also learn how to add a list to DataFrame, how to append a dictionary to DataFrame in Pandas, how to add rows from a list and more.
How to Set Up a Pandas DataFrame
Regarding library imports, you only need Pandas, so stick this line at the top of your script or notebook:
import pandas as pd
This is the data set we’ll use in the article. It contains four made-up employees described over three features:
employees = {
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
}
data = pd.DataFrame(employees)
data
And this is what it looks like:
Let’s start with the simplest way to add a row to a Pandas DataFrame, which is by using the append()
method.
1. Pandas Add Row to DataFrame Append() Method
You can use the append()
method from Pandas to add one or more rows to an existing DataFrame. The method is called on a Pandas DataFrame object, which means you must have one DataFrame already declared.
To add a single row to our data set, we’ll leverage Pandas to add a row to DataFrame from dict
. A new record is stored as a Python dictionary to a variable new_employee
. You then have to call the append()
method on the original data set and provide the new row value:
# A single new employee
new_employee = {"First Name": "Joseph", "Last Name": "Dune", "Email": "[email protected]"}
# Add a single row using the append() method
data = data.append(new_employee, ignore_index=True)
data
The ignore_index=True
argument means the DataFrame index will be reset after the new row is added. This is useful since we’re using a default index (range index), and we don’t want the sequence to break.
Here’s what the DataFrame looks like:
We now have a new row in the Pandas DataFrame, which means the append()
method did its job. Let’s see how it can handle inserting multiple rows.
Adding Multiple Rows to DataFrame with append()
If you want to use Pandas to add multiple rows to DataFrame from a list, you’ll want to use the append()
method.
The idea is to declare a list of dictionaries that follow the same pattern you saw in the previous section. Each list item represents one row that will be added to the Pandas DataFrame.
Here’s the entire code snippet for adding multiple rows to a Pandas DataFrame:
data = pd.DataFrame({
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
})
# A list of 3 new employees
new_employees = [
{"First Name": "Joseph", "Last Name": "Dune", "Email": "[email protected]"},
{"First Name": "Jackie", "Last Name": "Slash", "Email": "[email protected]"},
{"First Name": "Ginni", "Last Name": "Mars", "Email": "[email protected]"}
]
# Add all three using the append() method
data = data.append(new_employees, ignore_index=True)
data
Our data set now has six rows in total:
That’s all for the append()
method, and next, we’ll learn the ins and outs of the loc[]
indexer.
2. Pandas Add Row to DataFrame Loc[] Method
The loc[] indexer in Pandas allows you to access a row, group of rows or columns or a boolean array. This means you can use it to access particular rows and columns and their combinations, and in this case, to add one or more rows to the DataFrame.
You can use the loc[]
indexer in Pandas to append a dictionary to DataFrame. It involves a bit more creative thinking than append()
or concat()
, since loc[]
technically isn’t a method for that particular task.
To add a row to DataFrame using loc[]
, you need to index the row at the end of the DataFrame (using len(df)
), and assign a new row to it.
Here’s the code snippet:
data = pd.DataFrame({
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
})
# A single new employee
new_employee = {"First Name": "Jackie", "Last Name": "Slash", "Email": "[email protected]"}
# Add a single new employee using loc[]
data.loc[len(data)] = new_employee
data
The resulting DataFrame contains the new employee record now:
Adding one row is relatively easy, but what if you want to add more? Continue reading to find out.
Pandas DataFrame Insert Multiple Rows with loc[]
The loc[]
indexer doesn’t allow you to add multiple rows at once, so you’ll have to add row to DataFrame in a loop. That’s the only distinction when compared to adding a single row.
The following code snippet is simple to understand: We have a list of new employees that we iterate over, and then use the loc[]
indexer to add each employee individually to the DataFrame:
data = pd.DataFrame({
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
})
# A list of 3 new employees
new_employees = [
{"First Name": "Joseph", "Last Name": "Dune", "Email": "[email protected]"},
{"First Name": "Jackie", "Last Name": "Slash", "Email": "[email protected]"},
{"First Name": "Ginni", "Last Name": "Mars", "Email": "[email protected]"}
]
# All all three new employees using loc[]
for emp in new_employees:
data.loc[len(data)] = emp
data
The resulting Pandas DataFrame is identical to the one we had earlier:
Using loc[]
to add rows to Pandas DataFrame can be a lot of work. Let’s take a look at a more convenient approach next.
3. Panda Add Row to DataFrame pd.concat() Method
The concat()
method in Pandas is used to concatenate two Pandas DataFrame objects. Unlike append()
, it’s not a DataFrame method, and it belongs to the Pandas library instead.
This method requires both elements to be of type pd.DataFrame
, which means you’ll have to convert new records before concatenation. Not a dealbreaker, but just something to keep in mind.
Let’s begin by inserting a single row to a Pandas DataFrame with pd.concat()
. The new_employee
variable now contains a DataFrame object, which was created by wrapping a single employee dictionary into a Python list.
Then, you can use the pd.concat()
method and pass in a list of DataFrame objects you want to be concatenated. The default orientation is row-wise, meaning DataFrames will be stacked on top of each other (horizontally). The ignore_index=True
argument means the default range index will be reset after concatenation:
data = pd.DataFrame({
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
})
# A single new employee, converted to Pandas DataFrame from a list
new_employee = pd.DataFrame([{"First Name": "Ginni", "Last Name": "Mars", "Email": "[email protected]"}])
# Adding a single new employee row using pd.concat()
data = pd.concat([data, new_employee], ignore_index=True)
data
This is what the resulting DataFrame looks like:
Up next, let’s see what are the challenges of adding multiple rows to a Pandas DataFrame.
DataFrame Insert Multiple Rows with pd.concat()
Adding multiple rows is almost identical to adding a single row. Remember, you already have a new Pandas DataFrame that’s made from a list of dictionaries, so no one is stopping you from adding multiple records to that list.
Here’s an example in code:
data = pd.DataFrame({
"First Name": ["Bob", "Mark", "Jane", "Patrick"],
"Last Name": ["Doe", "Markson", "Swift", "Johnson"],
"Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]
})
new_employees = pd.DataFrame([
{"First Name": "Joseph", "Last Name": "Dune", "Email": "[email protected]"},
{"First Name": "Jackie", "Last Name": "Slash", "Email": "[email protected]"},
{"First Name": "Ginni", "Last Name": "Mars", "Email": "[email protected]"}
])
# Add all three employees with pd.concat()
data = pd.concat([data, new_employees], ignore_index=True)
data
You’re already familiar with the resulting DataFrame, but here it is:
And that’s how easy it is to use the pd.concat()
method.
Understanding How to Add a Row to Pandas DataFrame
There’s no shortage of ways to add one or more rows to a Pandas DataFrame. This article showed you three possible approaches, listed in no particular order.
So, which one is the best? There’s no correct answer to this question, but here’s our recommendation:
- Use
append()
if you simply want to add one or more rows to a Pandas DataFrame - Use
concat()
if you want to concatenate two, three, or even more Pandas DataFrame objects
The concat()
method is more convenient when you’re dealing with multiple DataFrames, and append()
shines for simpler use cases.
Frequently Asked Questions
How do you add a row to Pandas DataFrame?
There are three common methods to add a row to a Pandas DataFrame:
Append()
: This method allows you to add one or more rows to an existing DataFrame.Loc[]
: This method allows you to access a row, group of rows or columns or a boolean array and add it to a Pandas DataFrame.Concat()
: This method is used to concatenate two Pandas DataFrame objects and add them as a row.
Here is the code for the append()
method:
# A single new employee
new_employee = {"First Name": "Joseph", "Last Name": "Dune", "Email": "[email protected]"}
# Add a single row using the append() method
data = data.append(new_employee, ignore_index=True)
data
How do you create a Pandas DataFrame?
There are two simple steps to create a Pandas DataFrame.
- Import pandas.
import pandas as pd
- Enter data set script.
employees = { "First Name": ["Bob", "Mark", "Jane", "Patrick"], "Last Name": ["Doe", "Markson", "Swift", "Johnson"], "Email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"] } data = pd.DataFrame(employees) data