Pandas DataFrame Add Row
In data analysis you need to add new arrows to an existing Dataframe when new data is available to increase the size of the dataset.
Pandas Dataframe is flexible enought to add new rows to an existing Dataframe.
In this article, you will learn how to add new rows to a Dataframe using append()
and loc[]
methods.
- Using append() (Deprecated)
- Using concat() Method
- Using loc[] Method
- Adding Multiple Rows
- Conclusion
Table of Contents
1. Using append() (Deprecated)
append() method was famously used to add new rows to a Dataframe in Pandas. But it is deprecated since version 1.4.0.
However, lets see how it was used to add new rows to a Dataframe.
import pandas as pd
# Create a Dataframe
df = pd.DataFrame({'Name': ['John', 'Smith'],
'Age': [30, 25],
'City': ['New York', 'London']})
print(df)
# Add new row to Dataframe
new_row = {'Name': 'Adam', 'Age': 26, 'City': 'New York'}
df = df.append(new_row, ignore_index=True)
print(df)
Note: append() method is deprecated since version 1.4.0 and does not support in new versions of Pandas, use concat() or loc[] method instead.
2. Using concat() Method
The concat() method in Pandas is a powerful tool for concatenating DataFrames along a particular axis. Here is minimal (just for our use to add row) syntax of concat() method.
pd.concat(objs, axis=0,)
Here, objs is a sequence or mapping of Series or DataFrame objects, axis is the axis to concatenate along.
Here is an example of using concat() method to add new row to a Dataframe.
import pandas as pd
# Existing DataFrame
df_existing = pd.DataFrame({'Name': ['Alice', 'Bob'],
'Age': [25, 30]})
# New row data
new_data = pd.DataFrame({'Name': ['Charlie'], 'Age': [35]})
print("Original DataFrame:")
print(df_existing)
# 👇 Concatenate DataFrames
df_concatenated = pd.concat([df_existing, new_data], ignore_index=True)
print("\nNew DataFrame:")
print(df_concatenated)
Output:
Original DataFrame: Name Age 0 Alice 25 1 Bob 30 New DataFrame: Name Age 0 Alice 25 1 Bob 30 2 Charlie 35
3. Using loc[] Method
The loc[] or iloc[] indexer can be used to add a row at a specific location in the DataFrame.
The following example adds a new row at the end of the DataFrame.
import pandas as pd
# Existing DataFrame
df_existing = pd.DataFrame({'Name': ['Alice', 'Bob'],
'Age': [25, 30]})
# New row data
new_data = pd.Series({'Name': 'Charlie', 'Age': 35})
print("Original DataFrame:")
print(df_existing)
# 👇 Add a row using loc
df_existing.loc[len(df_existing)] = new_data
print("\nNew DataFrame:")
print(df_existing)
Output:
Original DataFrame: Name Age 0 Alice 25 1 Bob 30 New DataFrame: Name Age 0 Alice 25 1 Bob 30 2 Charlie 35
4. Adding Multiple Rows
Adding multiple rows at once is efficient using the concat method with a list of DataFrames or Series.
The following example adds two new rows to the DataFrame.
import pandas as pd
# Existing DataFrame
df_existing = pd.DataFrame({'Name': ['Alice', 'Bob'],
'Age': [25, 30]})
# New rows data
new_rows = pd.DataFrame({'Name': ['Charlie', 'David'], 'Age': [35, 40]})
print("Original DataFrame:")
print(df_existing)
# 👇 Concatenate DataFrames
df_concatenated = pd.concat([df_existing, new_rows], ignore_index=True)
print("\nNew DataFrame:")
print(df_concatenated)
Output:
Original DataFrame: Name Age 0 Alice 25 1 Bob 30 New DataFrame: Name Age 0 Alice 25 1 Bob 30 2 Charlie 35 3 David 40
Conclusion
Adding rows to a Pandas DataFrame is a crucial operation in data analysis workflows. With the deprecation of the append method, the concat method emerges as the recommended and versatile choice for row additions.
Happy coding!