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.