
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to append a list to a Pandas DataFrame using loc in Python?
The Dataframe.loc is used to access a group of rows and columns by label or a boolean array. We will append a list to a DataFrame using loc. Let us first create a DataFrame. The data is in the form of lists of team rankings for our example −
# data in the form of list of team rankings Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
Following is the row to be appended −
myList = ["Sri Lanka", 7, 30]
Append the above row in the form of list using loc() −
dataFrame.loc[len(dataFrame)] = myList
Example
Following is the code −
import pandas as pd # data in the form of list of team rankings Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) print"DataFrame...\n",dataFrame # row to be appended myList = ["Sri Lanka", 7, 30] # append the above row in the form of list using loc() dataFrame.loc[len(dataFrame)] = myList # display the update dataframe print"\nUpdated DataFrame after appending a row using loc...\n",dataFrame
Output
This will produce the following output
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
- Related Articles
- How to append a list to a Pandas DataFrame using append() in Python?
- How to append a list to a Pandas DataFrame using iloc in Python?
- How to append a list as a row to a Pandas DataFrame in Python?
- Python Pandas - How to append rows to a DataFrame
- How to append new rows to DataFrame using a Template In Python Pandas
- How to access pandas DataFrame elements using the .loc attribute?
- Append list of dictionaries to an existing Pandas DataFrame in Python
- How to create a pandas DataFrame using a list?
- How to create a pandas DataFrame using a list of lists?
- How to create a pandas DataFrame using a list of tuples?
- How to create a pandas DataFrame using a list of dictionaries?
- Python - How to group DataFrame rows into list in Pandas?
- How to append objects in a list in Python?
- How to append element in the list using Python?
- How to access pandas Series elements using the .loc attribute?

Advertisements