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 to a Pandas DataFrame using append() in Python?
To append a list to a Pandas DataFrame, we can use the append() method. However, note that append() is deprecated as of Pandas 1.4.0, and pd.concat() is now the recommended approach.
Creating the Initial DataFrame
Let's start by creating a DataFrame with team rankings ?
import pandas as pd
# Data in the form of list of team rankings
team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4, 65], ['South Africa', 5, 50]]
# Creating a DataFrame and adding columns
dataFrame = pd.DataFrame(team_data, 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
Using append() Method (Deprecated)
Here's how to append a list using the traditional append() method ?
import pandas as pd
# Original DataFrame
team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4, 65], ['South Africa', 5, 50]]
dataFrame = pd.DataFrame(team_data, columns=['Country', 'Rank', 'Points'])
# Row to be appended
new_row = [["Sri Lanka", 6, 40]]
# Append the row using append() method
dataFrame = dataFrame.append(pd.DataFrame(new_row, columns=['Country', 'Rank', 'Points']), ignore_index=True)
print("Updated DataFrame after appending:")
print(dataFrame)
Updated DataFrame after appending:
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
Using pd.concat() (Recommended)
The modern approach using pd.concat() is more efficient and recommended ?
import pandas as pd
# Original DataFrame
team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4, 65], ['South Africa', 5, 50]]
dataFrame = pd.DataFrame(team_data, columns=['Country', 'Rank', 'Points'])
# Row to be appended
new_row = [["Sri Lanka", 6, 40]]
new_df = pd.DataFrame(new_row, columns=['Country', 'Rank', 'Points'])
# Append using pd.concat()
dataFrame = pd.concat([dataFrame, new_df], ignore_index=True)
print("Updated DataFrame using pd.concat():")
print(dataFrame)
Updated DataFrame using pd.concat():
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
Comparison
| Method | Status | Performance | Best For |
|---|---|---|---|
append() |
Deprecated | Slower | Legacy code only |
pd.concat() |
Recommended | Faster | All new projects |
Conclusion
While append() can append lists to DataFrames, use pd.concat() for better performance and future compatibility. Both methods require converting the list to a DataFrame first and using ignore_index=True to reset row indices.
Advertisements
