- 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 add one row in an existing Pandas DataFrame?
While working with data using pandas in Python adding a new row (it could be one row or multiple rows) to an existing pandas Dataframe is a common task that can be performed using various pandas methods. Pandas is a popular data manipulation library in python that provides multiple functionalities for data analysis. In this article, we will be discussing how to add one row in an existing pandas dataframe in Python using different methods.
How to add one row in an existing Pandas dataframe?
Before we add a new row in the pandas dataframe, let us first create a sample Pandas Dataframe in Python that we will use throughout the article. We will create a Pandas DataFrame with three columns: “Name”,” Gender”, and “Age”. Below is the program to create the Pandas Dataframe in Python −
Example
import pandas as pd data = { "Name": ["Jane", "Martin", "Baskin"], "Gender": ["Female", "Male", "Male"], "Age": [20, 34, 32] } df = pd.DataFrame(data) print(df)
Output
Name Gender Age 0 Jane Female 20 1 Martin Male 34 2 Baskin Male 32
Now we will be discussing various methods that can be used to add one row in an existing Pandas dataframe in Python.
Method 1: Using append() method
Using the append() method is one of the simplest ways to add a new row to a Pandas Dataframe. This method appends the new row to an existing Dataframe. Below is an example of how to use this method with the existing dataframe −
Example
import pandas as pd data = { "Name": ["Jane", "Martin", "Baskin"], "Gender": ["Female", "Male", "Male"], "Age": [20, 34, 32] } df = pd.DataFrame(data) new_r = {"Name": "Alicia", "Gender": "Female", "Age": 30} df = df.append(new_r, ignore_index=True) print(df)
Output
Name Gender Age 0 Jane Female 20 1 Martin Male 34 2 Baskin Male 32 3 Alicia Female 30
In the above program, we have created a new dictionary called new_row with the values for the new row that we want to add to the existing dataframe. We then used the append() function to add the new row to the existing dataframe. The ignore_index=True argument is used to reset the index of the Dataframe after adding the new row.
Method 2: Using loc[] method
The other way to add a new row to a Pandas DataFrame is by using a loc[] method. This method allows us to select a specific location in the dataframe and add a new row at that location. Given below is one example of how to add a new row at the end of the Pandas DataFrame using the loc[] method −
Example
import pandas as pd d= { "Name": ["Jane", "Martin", "Baskin"], "Gender": ["Female", "Male", "Male"], "Age": [20, 34, 32] } df = pd.DataFrame(d) new_r = {"Name": "Alice", "Gender": "Female", "Age": 27} df.loc[len(df)] = new_r print(df)
Output
Name Gender Age 0 Jane Female 20 1 Martin Male 34 2 Baskin Male 32 3 Alice Female 27
In the above code, we used the len(df) method of the pandas to fetch the index of the last row in the DataFrame and added one to get the index of the new row. We after that used the loc[] method to add the new row to the end of the existing DataFrame.
Add multiple rows to an existing DataFrame in Python
If we want to add multiple rows to an existing Pandas DataFrame, we can create a new DataFrame containing the new rows and we will be using the concat() method to concatenate a new dataframe to an existing dataframe. Below is an example to do the same −
Example
import pandas as pd d= { "Name": ["Jane", "Martin", "Baskin"], "Gender": ["Female", "Male", "Male"], "Age": [20, 34, 32] } df = pd.DataFrame(d) new_d = { 'Name': ['Alaska', 'David'], 'Age': [28, 33], 'Gender': ['female', 'Male'] } new_df = pd.DataFrame(new_d) df = pd.concat([df, new_df], ignore_index=True) print(df)
Output
Name Gender Age 0 Jane Female 20 1 Martin Male 34 2 Baskin Male 32 3 Alaska female 28 4 David Male 33
In the above program, we have created a new DataFrame containing the new rows that we want to add to the existing DataFrame. After that, we used the concat() method of the Pandas library of Python to concatenate the new dataframe to the existing dataframe that we have already created. The ignore_index=True parameter or the argument is used to reset the index of the Dataframe after the concatenation of the new dataframe to the existing dataframe.
Conclusion
In conclusion, we have used three different functions of the pandas to add one row in the existing Pandas Dataframe. We have used concat() method, loc[] method, and the append() function to add one row to the existing dataframe created using Panads which is a library in python.