Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add one row in an existing Pandas DataFrame?
While working with data using Pandas in Python, adding a new row to an existing DataFrame is a common task that can be performed using various methods. Pandas is a popular data manipulation library that provides multiple functionalities for data analysis. In this article, we will discuss how to add one row in an existing Pandas DataFrame using different methods.
Sample DataFrame
Before we add a new row to the Pandas DataFrame, let's first create a sample DataFrame that we will use throughout the article. We will create a DataFrame with three columns: "Name", "Gender", and "Age".
import pandas as pd
data = {
"Name": ["Jane", "Martin", "Baskin"],
"Gender": ["Female", "Male", "Male"],
"Age": [20, 34, 32]
}
df = pd.DataFrame(data)
print(df)
Name Gender Age
0 Jane Female 20
1 Martin Male 34
2 Baskin Male 32
Method 1: Using concat() Method (Recommended)
The concat() method is the recommended approach for adding rows to a DataFrame. It concatenates a new DataFrame with the existing one ?
import pandas as pd
data = {
"Name": ["Jane", "Martin", "Baskin"],
"Gender": ["Female", "Male", "Male"],
"Age": [20, 34, 32]
}
df = pd.DataFrame(data)
# Create a new row as a DataFrame
new_row = pd.DataFrame({"Name": ["Alicia"], "Gender": ["Female"], "Age": [30]})
df = pd.concat([df, new_row], ignore_index=True)
print(df)
Name Gender Age
0 Jane Female 20
1 Martin Male 34
2 Baskin Male 32
3 Alicia Female 30
Method 2: Using loc[] Method
The loc[] method allows us to add a new row at a specific location in the DataFrame. We use len(df) to add the row at the end ?
import pandas as pd
data = {
"Name": ["Jane", "Martin", "Baskin"],
"Gender": ["Female", "Male", "Male"],
"Age": [20, 34, 32]
}
df = pd.DataFrame(data)
# Add a new row using loc[]
df.loc[len(df)] = ["Alice", "Female", 27]
print(df)
Name Gender Age
0 Jane Female 20
1 Martin Male 34
2 Baskin Male 32
3 Alice Female 27
Method 3: Using append() Method (Deprecated)
Note: The append() method is deprecated as of Pandas 1.4.0. Use concat() instead. Here's how it was used ?
import pandas as pd
data = {
"Name": ["Jane", "Martin", "Baskin"],
"Gender": ["Female", "Male", "Male"],
"Age": [20, 34, 32]
}
df = pd.DataFrame(data)
# Deprecated method - use concat() instead
new_row = {"Name": "Alicia", "Gender": "Female", "Age": 30}
df = df.append(new_row, ignore_index=True)
print(df)
Adding Multiple Rows
To add multiple rows simultaneously, create a DataFrame with multiple rows and use concat() ?
import pandas as pd
data = {
"Name": ["Jane", "Martin", "Baskin"],
"Gender": ["Female", "Male", "Male"],
"Age": [20, 34, 32]
}
df = pd.DataFrame(data)
# Create multiple new rows
new_rows = pd.DataFrame({
'Name': ['Alaska', 'David'],
'Gender': ['Female', 'Male'],
'Age': [28, 33]
})
df = pd.concat([df, new_rows], ignore_index=True)
print(df)
Name Gender Age
0 Jane Female 20
1 Martin Male 34
2 Baskin Male 32
3 Alaska Female 28
4 David Male 33
Comparison
| Method | Status | Best For | Performance |
|---|---|---|---|
concat() |
Recommended | Single or multiple rows | Good |
loc[] |
Active | Single row with known position | Excellent |
append() |
Deprecated | Legacy code only | Poor |
Conclusion
Use concat() for adding single or multiple rows to a DataFrame as it's the current recommended approach. The loc[] method is efficient for adding single rows when you know the exact position.
