How to add or insert a row to pandas DataFrame? You can use multiple ways of Pandsa such as append(), pandas.concat(), and loc[]. In this article, I will explain how to add a row to DataFrame with several examples.
The examples include inserting or adding a Python list, dict (dictionary) as a row to pandas DataFrame, which ideally adds a new record to the DataFrame with elements specified by a list and dict.
Quick Examples of Adding Row to DataFrame
If you are in a hurry, below are some quick examples of how to insert/add a row to DataFrame.
# Quick examples of adding row to dataframe
# Example 1: Add Row to DataFrame
list_row = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list_row
# Example 2: Insert Dict as 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)
# Example 3: Add 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 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)
# Example 6: Add specific row/index name
# Using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]
# Example 7: Add row in DataFrame
# Using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]
Now, let’s create a DataFrame with some rows and columns and then execute the examples provided to validate the results.
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.
Add Row to Pandas DataFrame
To add or insert a row to an existing pandas DataFrame from a dictionary, you can use the append()
function which takes a ignore_index=True
parameter to add a dictionary as a row to the DataFrame. If you do not pass this parameter, an error will be returned. However, if you pass this parameter into this function will return the updated DataFrame with the newly added row.
# 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("After adding a new row to DataFrame:\n", df2)
Yields below output.
Note that when you used ignore_index=True
, it ignores the existing index on the DataFrame and set a new index.
Add or Insert List as Row to DataFrame
To add/insert a list as a row to a DataFrame, you can use the loc[]
accessor with the length of the DataFrame as the index for the new row. For instance, you define a new row as a list list
. Then using df.loc[len(df)] = list
, you append the new row to the DataFrame using its length as the index for the new row.
# Add list as row to dataframe
list = ["Hyperion", 24000, "55days", 1800]
df.loc[len(df)] = list
print("After adding a new row to DataFrame:\n", df)
Yields below output.
# Output:
After adding a new 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
7 Hyperion 24000 55days 1800
Note that when you have a default number index, it automatically increments the index and adds the row at the end of the DataFrame.
Add Row to DataFrame with Index Name
Alternatively, you can also use DataFrame.append()
function to add a new row to pandas DataFrame with a custom Index. Note that in this example, first, you create a new DataFrame or Series and then append it to another DataFrame.
# Add new row to specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))
print("After adding a new row to DataFrame:\n", df2)
# Add row to the DataFrame using append()
df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))
print("After adding a new row to DataFrame:\n", df2)
Yields below output.
# Output:
After adding a new 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
7 Hyperion 24000 55days 1800
Use concat() to Add Row at the Top of DataFrame
You can use pd.concat()
to add a row to the first position of the Pandas DataFrame with the index position as 0. The reset_index()
function will reset the index on the DataFrame to adjust the indexes on other rows.
# 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("After adding a new row to DataFrame:\n", df2)
Yields below output.
# Output:
After adding a new 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 Add Specific Row/Index Name
By using df.loc[Index_label]=new_row
, you can add/insert a list as a row to the DataFrame at a specific index label. Let’s add the list as a row to DataFrame at the last index position using DataFrame.loc['Index5',:]
function.
# Add specific row/index name using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]
print("After adding a new row to DataFrame:\n", df)
# Add row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]
print("After adding a new row to DataFrame:\n", df)
In the above example, we use df.loc['7', :]
to specify the row with the index name ‘7’ and assign the values ['Hive', 25000, '45days', 2000]
to it. This adds a new row with the index name ‘7’ to the DataFrame df
with the specified values. This example yields the below output.
# Output:
After adding a new 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
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("After adding a new row to DataFrame:\n", df2)
# Add 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("After adding a new row to DataFrame:\n", df2)
# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='Index'))
print("After adding a new row to DataFrame:\n", 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("After adding a new row to DataFrame:\n", df2)
# Add specific row/index name using DataFrame.loc[]
df.loc['Index5', :] = ['Hive',25000,'45days',2000]
print("After adding a new row to DataFrame:\n", df)
# Add row in DataFrame using DataFrame.loc[]
df.loc['Index5'] = ['Hive',25000,'45days',2000]
print("After adding a new row to DataFrame:\n", df)
FAQ of Add or Insert Row to DataFrame
You can use pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
to add the row to the first position of the Pandas DataFrame with the index position as 0. The reset_index()
function will reset the index on the DataFrame to adjust the indexes on other rows.
append()
is a method of the DataFrame class and is used to add a single row to the end of the DataFrame. concat()
is a function that can be used to concatenate two or more DataFrames along a specified axis, which can be used to add rows at various positions in the DataFrame.
To add multiple rows efficiently, you can create a new DataFrame with the rows you want to add and then use concat
it to concatenate it with the original DataFrame.
You can add a row at a specific position to the DataFrame by using df.loc[Index_label]=new_row
this syntax.
Conclusion
In this article, I have explained how to add or insert a row to Pandas DataFrame using loc[]
, concat()
, and append()
methods. 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
- Pandas get the number of rows
- Pandas add header row to DataFrame
- Pandas count rows with condition
- Pandas sum DataFrame rows with examples
- Get unique rows in Pandas DataFrame
- Get the first N rows of Pandas DataFrame
- Pandas filter rows by conditions
- Pandas append rows & columns to empty DataFrame
- Pandas select DataFrame rows between two dates
- Pandas get a list of all duplicate rows
- Select rows from list of values in Pandas DataFrame
- How to append a list as the row to DataFrame?
- Pandas get the number of rows from DataFrame
As in Pandas 2.0, the method append was deprecated and removed. One should use either concat or loc