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 append a list to a Pandas DataFrame using iloc in Python?
The iloc method in Pandas provides integer-location based indexing for selecting and modifying DataFrame rows by position. While iloc is primarily used for selection, it can also be used to replace existing rows with new data from a list.
Creating a Sample DataFrame
Let's start by creating a DataFrame with team ranking data ?
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], ['Bangladesh', 6, 40]]
# Creating a DataFrame with column names
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
5 Bangladesh 6 40
Replacing a Row Using iloc
We can use iloc to replace an existing row with data from a list. Note that this replaces the existing row rather than truly "appending" ?
import pandas as pd
# Create the DataFrame
team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75],
['New Zealand', 4, 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]]
dataFrame = pd.DataFrame(team_data, columns=['Country', 'Rank', 'Points'])
# List to replace existing row
new_team = ["Sri Lanka", 7, 30]
# Replace row at index 5 (Bangladesh) with new data
dataFrame.iloc[5] = new_team
print("DataFrame after replacing row at index 5:")
print(dataFrame)
DataFrame after replacing row at index 5:
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 7 30
Alternative: True Appending with loc
To actually append a new row (rather than replace), use loc with the next available index ?
import pandas as pd
# Create the DataFrame
team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75]]
dataFrame = pd.DataFrame(team_data, columns=['Country', 'Rank', 'Points'])
print("Original DataFrame:")
print(dataFrame)
# Append new row using loc
new_team = ["Sri Lanka", 4, 60]
dataFrame.loc[len(dataFrame)] = new_team
print("\nDataFrame after appending:")
print(dataFrame)
Original DataFrame:
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
DataFrame after appending:
Country Rank Points
0 India 1 100
1 Australia 2 85
2 England 3 75
3 Sri Lanka 4 60
Key Points
Important considerations when using iloc with lists:
-
ilocreplaces existing rows, not truly appending - The list length must match the number of DataFrame columns
- Use
loc[len(df)]orpd.concat()for true appending - Index must exist for
ilocassignment to work
Conclusion
While iloc can modify DataFrame rows with list data, it replaces existing rows rather than appending. For true row addition, use loc with a new index or pd.concat() method.
