- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to append a list as a row to a Pandas DataFrame in Python?
To open a list, we can use append() method. With that, we can also use loc() method. At first, let us 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 −
dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
Let’s say the following is the row to be appended −
myList = [["Sri Lanka", 6, 40]]
Append the above row in the form of list −
dataFrame = dataFrame.append(pd.DataFrame(myList, columns=['Country', 'Rank', 'Points']), ignore_index=True)
Example
Following is the code to append using append() −
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]] # 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", 6, 40]] # append the above row in the form of list dataFrame = dataFrame.append(pd.DataFrame(myList, columns=['Country', 'Rank', 'Points']), ignore_index=True) # display the update dataframe print"\nUpdated DataFrame after appending a row...\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 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
Let’s see another example −
Example
Following is the code to append using the loc() method −
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
Advertisements