Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Science

How to Add Rows in Pandas DataFrame? (with code)

Piyush Kaushal by Piyush Kaushal
December 13, 2023
Reading Time: 4 mins read
Pandas Add Rows to DataFrames
Follow us on Google News   Subscribe to our newsletter

One common task in data analysis with Pandas is adding or inserting a new row to an existing DataFrame. Whether you need to add a single row or multiple rows, Pandas provides a lot of techniques for this.  In this article, we will explore different techniques for adding rows to a Pandas DataFrame using Pandas.

Let us start by exploring the various ways we can add a row to a DataFrame.

How to Add Single Row in Pandas DataFrame?

There are various scenarios where adding rows becomes necessary, such as appending new records to an existing dataset or incorporating new observations into an analysis.

We can add a single row to a DataFrame using the following methods:

1) Using DataFrame.loc

One way to add a single row to a DataFrame is by using the DataFrame.loc method. This method allows us to specify the position where the new row should be inserted. 

To add a row at the end of the DataFrame, we can simply utilize the length of the index of the DataFrame to determine the position.

Let us try an example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })

# Display the DataFrame
print('Original DataFrame:\n', df)

# Add a new row
df.loc[len(df.index)] = ['Amy', 89, 93]

# Display the New DataFrame
print('New DataFrame:\n', df)

Output:

Original DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76

New DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76
4      Amy     89       93

2) Using DataFrame.append

Another method to add a single row to a DataFrame is by using the DataFrame.append function. This method allows us to append a new row as a dictionary or a Series. 

We can set the ignore_index parameter to True, and it will automatically assign a new index to the appended row.

Let us see an example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })

# Display the DataFrame
print('Original DataFrame:\n', df)

# Create a new row as a dictionary
new_row = {'Name': 'Amy', 'Maths': 89, 'Science': 93}

# Append the new row to the DataFrame
df = df.append(new_row, ignore_index=True)

# Display the New DataFrame
print('New DataFrame:\n', df)

Output:

Original DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76

New DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76
4      Amy     89       93

How to Add Multiple Rows in a DataFrame?

We can also add multiple rows to our DataFrame. The need to add multiple rows can arise when we want to merge two DataFrames.

We can use the pandas.concat function to join or merge two DataFrames. This concat method involves creating a new DataFrame containing all the rows that need to be added, and then concatenating it with the original DataFrame.

Here is how to do it:

import pandas as pd

# Create the original DataFrame
df1 = pd.DataFrame({'Name':['Martha', 'Tim', 'Rob', 'Georgia'], 'Maths':[87, 91, 97, 95], 'Science':[83, 99, 84, 76] })

# Display the DataFrame
print('Original DataFrame:\n', df1)

# Create a new DataFrame with the rows to be added
df2 = pd.DataFrame({'Name':['Amy', 'Maddy'], 'Maths':[89, 90], 'Science':[93, 81] })

# Concatenate the two DataFrames
df3 = pd.concat([df1, df2], ignore_index=True)

# Display the New DataFrame
print('New DataFrame:\n', df)

Output:

Original DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76

New DataFrame:
       Name  Maths  Science
0   Martha     87       83
1      Tim     91       99
2      Rob     97       84
3  Georgia     95       76
4      Amy     89       93

Conclusion

In this article, we explored the various methods we can use to add a single or multiple rows to a Pandas DataFrame. By leveraging the capabilities of Pandas, we can easily add new rows to our DataFrame and incorporate additional data into our analyses. Whether we need to append a single observation or insert multiple records, Pandas offers flexible solutions to meet our needs.

ShareTweetShareSendSend
Piyush Kaushal

Piyush Kaushal

I am Piyush Kaushal, currently pursuing a degree in software engineering at a prestigious government university. I am dedicated to staying informed about the latest technological developments and continuously expanding my knowledge base. I take great pleasure in sharing my expertise in data science and coding with fellow aspiring minds.

RelatedPosts

Moving Average in Pandas

Calculate Moving Average in Pandas (with code)

January 12, 2024
Pandas Convert Datetime to Date Column

Convert Datetime to Date Column in Pandas (with code)

January 4, 2024
Convert Pandas DataFrame to NumPy Array

Convert Pandas DataFrame to NumPy Array (with code)

January 3, 2024
Pandas DataFrame isna() Method

Pandas DataFrame isna() Method Explained

January 3, 2024
Pandas DataFrame copy() Method

Pandas DataFrame copy() Method Explained

January 1, 2024

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.