You can append one row or multiple rows to an existing pandas DataFrame in several ways, one way would be creating a list or dict with the details and appending it to DataFrame.
You can append a row to DataFrame by using append(), pandas.concat(), and loc[]. In this article, I will explain how to append a Python list, dict (dictionary) as a row to Pandas DataFrame, which ideally inserts a new row(s) to the DataFrame with elements specified by a list and dict.
Key Points –
- Use the
append()
method to add a row to a pandas DataFrame. - Specify the
ignore_index=True
parameter to reset the index of the appended DataFrame. - Use the
loc
accessor to append a row to a Pandas DataFrame. - Create a new DataFrame row as a dictionary with column names as keys and corresponding values.
- Utilize the
len()
function to determine the index for the new row.
Quick Examples of Appending
If you are in a hurry, below are some quick examples of appending a row to Pandas DataFrame.
# Below are some quick examples
# Example 1: Append Row to DataFrame
list_row = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list_row
# Example 2: Insert Dict to the dataframe using DataFrame.append()
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)
# Example 3: Append new row to specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))
# Example 4: Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))
# Example 5: Using pandas.concat() to append a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
# Example 6: Append specific row/index name using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]
# Example 7: Append row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]
Let’s create a pandas DataFrame from Dict with a few rows and columns and execute some examples to learn how to insert rows. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create DataFrame
import pandas as pd
technologies = ({
'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
'Fee' :[20000,25000,30000,22000,26000],
'Duration':['30days','40days','35days','60days','50days'],
'Discount':[1000,2500,1500,1200,3000]
})
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
Yields below output.
Append Dict as Row to DataFrame
You can append a new row to the existing DataFrame from the dictionary. First, create a dictionary, and then apply the append()
function, this function is required to pass ignore_index=True
in order to append dict as a row to DataFrame, not using this will get you an error.
# Insert row to the dataframe using DataFrame.append()
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)
print("After appending a row to DataFrame:\n", df2)
Yields below output.
Append List to DataFrame
If you have a list and want to append it to DataFrame use loc[]
attribute. For more similar examples, refer to how to append a list as a row to pandas DataFrame.
# New list to append Row to DataFrame
list = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list
print("After appending a row to DataFrame:\n", df)
The output same as the above.
Append Row at the Specific Index Name
Note that in this example, first we are creating a new DataFrame/Series and append this to another DataFrame at the specified index.
# Append new row by specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['5'],columns=df.columns))
print("After appending a row to DataFrame:\n", df)
# Append row to the DataFrame using append()
df2 = df.append(pd.Series(new_row, index=df.columns, name='5'))
print("After appending a row to DataFrame:\n", df)
# Output:
# After appending a row to DataFrame:
# Courses Fee Duration Discount
# 0 Spark 20000 30days 1000
# 1 Hadoop 25000 40days 2500
# 2 pandas 30000 35days 1500
# 3 Java 22000 60days 1200
# 4 Pyspark 26000 50days 3000
# 5 Hyperion 24000 55days 1800
Use concat() Function to Append
Use pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
to append the row to the first position of the DataFrame as the Index starts from zero.
# Using pandas.concat() to append a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
print("After appending a row to DataFrame:\n", df)
# Output:
# After appending a row to DataFrame:
# Courses Fee Duration Discount
# 0 Hyperion 24000 55days 1800
# 1 Spark 20000 30days 1000
# 2 Hadoop 25000 40days 2500
# 3 pandas 30000 35days 1500
# 4 Java 22000 60days 1200
# 5 Pyspark 26000 50days 3000
Use DataFrame.loc[] to Append Row
By using df.loc[Index_label] = new_row
you can append a list as a row to the DataFrame at a specified index.
# Append specific row/index name using DataFrame.loc[]
df.loc['5', :] = ['Hive',25000,'45days',2000]
print("After appending a row to DataFrame:\n", df)
# Append row in DataFrame using DataFrame.loc[]
df.loc['5'] = ['Hive',25000,'45days',2000]
print("After appending a row to DataFrame:\n", df)
# Output
# After appending a row to DataFrame:
# Courses Fee Duration Discount
# 0 Spark 20000.0 30days 1000.0
# 1 Hadoop 25000.0 40days 2500.0
# 2 pandas 30000.0 35days 1500.0
# 3 Java 22000.0 60days 1200.0
# 4 Pyspark 26000.0 50days 3000.0
# 7 Hive 25000.0 45days 2000.0
Complete Example of Append Row to DataFrame
import pandas as pd
technologies = ({
'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
'Fee' :[20000,25000,30000,22000,26000],
'Duration':['30days','40days','35days','60days','50days'],
'Discount':[1000,2500,1500,1200,3000]
})
df = pd.DataFrame(technologies)
print(df)
# Insert row to the dataframe using DataFrame.append()
df = pd.DataFrame(technologies)
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)
print(df2)
# Append new row to specifig index name
new_row = {'Courses':'Oracle','Fee':25000,'Duration':'65days','Discount':2800}
df2 = df.append(pd.DataFrame([new_row],index=['Index'],columns=df.columns))
print(df2)
# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='Index'))
print(df2)
# Using pandas.concat() to add a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
print (df2)
# Add specific row/index name using DataFrame.loc[]
df.loc['Index5', :] = ['Hive',25000,'45days',2000]
print(df)
# Add row in DataFrame using DataFrame.loc[]
df.loc['Index5'] = ['Hive',25000,'45days',2000]
print(df)
Frequently Asked Questions on Append Row to DataFrame
To append a row to a Pandas DataFrame, you can use the append()
method. For example, df = df.append(new_row, ignore_index=True)
.
The ignore_index
parameter is a boolean flag. When it is set to True
, it will reindex the DataFrame after appending the row, so the index will be consecutive numbers starting from 0. If it is set to False
, the index of the appended row will be retained.
The append
method is simple to use, but it may not be the most efficient for adding multiple rows to a DataFrame. If you need to append multiple rows, it’s often more efficient to create a list of rows as DataFrames and then concatenate them using pd.concat
.
Pandas does not provide a direct method to append a row to a specific position in a DataFrame. To insert a row at a specific location, you can create a new DataFrame with the desired order and then concatenate it.
To append a row with a custom index, you can set the index of the row DataFrame before appending it to the main DataFrame. For example, new_row = pd.DataFrame({'A': 1, 'B': 2}, index=[custom_index]) df = df.append(new_row)
Conclusion
In this article, you have learned how to append a row to DataFrame using loc[]
, concat()
, and append()
functions. Using these you can add a row from a list or dictionary at any position/index in the DataFrame with examples.
Happy Learning !!
Related Articles
- Empty Pandas DataFrame with Specific Column Types
- Rename Specific Columns in Pandas DataFrame
- Create Pandas DataFrame With Working Examples
- Get Column Average or Mean in Pandas DataFrame
- Retrieve Number of Rows From Pandas DataFrame
- Set Order of Columns in Pandas DataFrame
- How to Append Pandas Series?
- Append Pandas DataFrames Using for Loop
- How to Union Pandas DataFrames using Concat?
- Pandas Create New DataFrame By Selecting Specific Columns