Skip to content

Add Rows to DataFrame in Python

[

How to Add Rows to a DataFrame in Python

In Python, the Pandas library provides powerful tools for data manipulation and analysis, including the ability to work with tabular data using DataFrames. One common task when working with DataFrames is adding new rows to an existing DataFrame. In this tutorial, we will explore different methods to accomplish this task, step by step, with executable code samples.

Method 1: Using the append() function

Pandas DataFrame has a built-in function called append() that allows you to add one or more rows to an existing DataFrame. Here’s an example using this method:

import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Name': ['John', 'Emma', 'Michael'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']})
# Create a new row as a dictionary
new_row = {'Name': 'Emily', 'Age': 28, 'City': 'Berlin'}
# Append the new row to the DataFrame
df = df.append(new_row, ignore_index=True)
# Display the modified DataFrame
df

In this example, we first create a DataFrame with three columns: ‘Name’, ‘Age’, and ‘City’. Then, we define a new row as a dictionary. Finally, we use the append() function to add the new row to the DataFrame. The ignore_index=True argument ensures that the index of the new row is automatically assigned.

Method 2: Using the loc[] accessor

Another method to add rows to a DataFrame is by using the loc[] accessor along with the index label of the new row. Here’s an example:

import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Name': ['John', 'Emma', 'Michael'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']})
# Create a new row as a Series
new_row = pd.Series(['Emily', 28, 'Berlin'], index=df.columns)
# Add the new row using loc[]
df.loc[len(df)] = new_row
# Display the modified DataFrame
df

In this example, we first create a DataFrame with the same structure as before. Then, we create a new row as a Series, ensuring that the Series index matches the DataFrame column names. Finally, we add the new row to the DataFrame using the loc[] accessor with the length of the DataFrame as the index label.

Method 3: Using the concat() function

The concat() function in Pandas allows you to concatenate multiple DataFrames along a particular axis. By using this function, you can also add rows to an existing DataFrame. Here’s an example:

import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'Name': ['John', 'Emma', 'Michael'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']})
# Create a new DataFrame with the row to be added
new_df = pd.DataFrame({'Name': ['Emily'],
'Age': [28],
'City': ['Berlin']})
# Concatenate the two DataFrames
df = pd.concat([df, new_df], ignore_index=True)
# Display the modified DataFrame
df

In this example, we first create a DataFrame as before. Then, we create a new DataFrame with the row to be added. We use the concat() function to concatenate the two DataFrames along the row axis (axis=0). The ignore_index=True argument ensures that the resulting DataFrame has a new index.

Conclusion

In this tutorial, we explored three different methods to add rows to an existing DataFrame in Python using the Pandas library. We learned how to use the append() function, the loc[] accessor, and the concat() function, along with detailed step-by-step code samples. By mastering these techniques, you can efficiently manipulate and expand your data within DataFrames, making your data analysis workflows more powerful and flexible.