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
Selected Reading
How to append a list as a row to a Pandas DataFrame in Python?
To append a list as a row to a Pandas DataFrame in Python, we can use the concat() method (recommended) or the loc[] indexer. Let's first import the required library ?
import pandas as pd
Following is the data in the form of lists of team rankings ?
Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50]]
Creating a DataFrame with the above data and adding columns ?
import pandas as pd
Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50]]
dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
print("Original DataFrame...")
print(dataFrame)
Original DataFrame...
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
3 New Zealand 4 65
4 South Africa 5 50
Method 1: Using concat() (Recommended)
The modern approach is to use concat() instead of the deprecated append() method ?
import pandas as pd
# Original DataFrame
Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50]]
dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
# Row to be appended as a list
myList = [["Sri Lanka", 6, 40]]
# Create new DataFrame from the list and concatenate
new_row = pd.DataFrame(myList, columns=['Country', 'Rank', 'Points'])
dataFrame = pd.concat([dataFrame, new_row], ignore_index=True)
print("Updated DataFrame after appending a row...")
print(dataFrame)
Updated DataFrame after appending a row...
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
3 New Zealand 4 65
4 South Africa 5 50
5 Sri Lanka 6 40
Method 2: Using loc[] Indexer
You can also use the loc[] method to append a single row directly ?
import pandas as pd
# Original DataFrame
Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]]
dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
print("DataFrame...")
print(dataFrame)
# Row to be appended as a list (single row, not nested)
myList = ["Sri Lanka", 7, 30]
# Append using loc[] - assign to next available index
dataFrame.loc[len(dataFrame)] = myList
print("\nUpdated DataFrame after appending a row using loc...")
print(dataFrame)
DataFrame...
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
3 New Zealand 4 65
4 South Africa 5 50
5 Bangladesh 6 40
Updated DataFrame after appending a row using loc...
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
3 New Zealand 4 65
4 South Africa 5 50
5 Bangladesh 6 40
6 Sri Lanka 7 30
Comparison
| Method | Performance | Best For |
|---|---|---|
concat() |
Better for multiple rows | Modern, recommended approach |
loc[] |
Faster for single row | Simple single row addition |
Conclusion
Use pd.concat() for appending lists as rows to DataFrames, as it's the modern recommended approach. Use loc[] for simple single-row additions when performance matters.
Advertisements
