- How to Add or Append Row to Pandas DataFrame?
- Syntax of append()
- Examples
- 1. Add Row to DataFrame
- 2. Add Row to Pandas DataFrame (ignoreIndex = False)
- Summary
- Pandas Add Row to DataFrame – Definitive Guide
- Creating an Empty Dataframe
- Add row Using Append
- Add row Using Concat
- Add row Using iLOC
- Add row Using LOC
- Pandas Insert Empty Row
- Why You Should Not Add Rows One By One To Dataframe
- Conclusion
- Additional Resources
How to Add or Append Row to Pandas DataFrame?
To append or add a row to DataFrame, create the new row as Series and use DataFrame.append() method.
In this tutorial, we shall learn how to append a row to an existing DataFrame, with the help of illustrative example programs.
Syntax of append()
The syntax of DataFrame.append() function to append a row new_row to the DataFrame mydataframe is
mydataframe.append(new_row, ignore_index=True)
where the resulting DataFrame contains new_row added to mydataframe.
append() is immutable. It does not change the original DataFrame, but returns a new DataFrame created from the given DataFrame with the row appended. In order to update the original DataFrame, assign the DataFrame returned by the append() function back to the original DataFrame.
mydataframe = mydataframe.append(new_row, ignore_index=True)
Examples
1. Add Row to DataFrame
In this example, we will create a DataFrame and append a new row to this DataFrame. The new row is initialized as a Python Dictionary and append() function is used to append the row to the dataframe.
When you are adding a Python Dictionary to append(), make sure that you pass ignore_index=True.
The append() method returns the dataframe with the newly added row.
Python Program
import pandas as pd data = #create dataframe df_marks = pd.DataFrame(data) print('Original DataFrame\n------------------') print(df_marks) new_row = #append row to the dataframe df_marks = df_marks.append(new_row, ignore_index=True) print('\n\nNew row added to DataFrame\n--------------------------') print(df_marks)
Run the above Python program, and you shall see the original dataframe, and the dataframe appended with the new row.
Original DataFrame ------------------ name physics chemistry algebra 0 Somu 68 84 78 1 Kiku 74 56 88 2 Amol 77 73 82 3 Lini 78 69 87 New row added to DataFrame -------------------------- name physics chemistry algebra 0 Somu 68 84 78 1 Kiku 74 56 88 2 Amol 77 73 82 3 Lini 78 69 87 4 Geo 87 92 97
2. Add Row to Pandas DataFrame (ignoreIndex = False)
If you do not provide the parameter ignoreIndex=False, you will get TypeError.
In the following example, we will try to append a row to DataFrame with the parameter ignoreIndex=False.
Python Program
import pandas as pd data = #create dataframe df_marks = pd.DataFrame(data) print('Original DataFrame\n------------------') print(df_marks) new_row = #append row to the dataframe df_marks = df_marks.append(new_row, ignore_index=False) print('\n\nNew row added to DataFrame\n--------------------------') print(df_marks)
Original DataFrame ------------------ name physics chemistry 0 Amol 77 73 1 Lini 78 85 Traceback (most recent call last): File "example1.py", line 14, in df_marks = df_marks.append(new_row, ignore_index=False) File "C:\Users\PythonExamples\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 6658, in append raise TypeError('Can only append a Series if ignore_index=True' TypeError: Can only append a Series if ignore_index=True or if the Series has a name
As the error message says, we need to either provide the parameter ignore_index=True or append the row, that is Series with a name.
We have already seen in Example 1, how to add row to the DataFrame with ignore_index=True. Now we will see how to add a row with ignore_index=False.
Python Program
import pandas as pd data = #create dataframe df_marks = pd.DataFrame(data) print('Original DataFrame\n------------------') print(df_marks) new_row = pd.Series(data=, name='x') #append row to the dataframe df_marks = df_marks.append(new_row, ignore_index=False) print('\n\nNew row added to DataFrame\n--------------------------') print(df_marks)
We have named the Series as data. Therefore ignore_index=False does not return a TypeError and the row is appended to the DataFrame.
Original DataFrame ------------------ name physics chemistry 0 Amol 77 73 1 Lini 78 85 New row added to DataFrame -------------------------- name physics chemistry 0 Amol 77 73 1 Lini 78 85 x Geo 87 92
Summary
In this Pandas Tutorial, we have used append() function to add a new row to Pandas DataFrame.
Pandas Add Row to DataFrame – Definitive Guide
Pandas dataframe is a two-dimensional data structure.
You can add rows to the pandas dataframe using df.iLOC[i] = [‘col-1-value’, ‘col-2-value‘, ‘ col-3-value ‘] statement.
If you’re in Hurry
You can use the following to add rows to the dataframe.
- It adds the rows to the dataframe using a dictionary.
- It inserts the row at the end of the dataframe.
dict = df = df.append(dict, ignore_index = True) df
Dataframe Will Look Like
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to add rows to a dataframe. You’ll also learn how to insert a row into an empty dataframe.
Creating an Empty Dataframe
First, you need to create an empty dataframe to add rows to it. You can do it by using DataFrame() method as shown below.
import pandas as pd df = pd.DataFrame() df
An empty dataframe is created as df .
You can add rows to the dataframe using four methods. append() , concat() , iloc[] and loc[] .
Add row Using Append
The append() method appends a row to an existing dataframe.
- dictionary or Pandas Series or Dataframe – Object with values for new row
- ignore_index = True Means the index from the series or the source dataframe will be ignored. The index available in the target dataframe will be used instead. False means otherwise. This is optional.
inplace append is not possible. Hence, do not forget to assign the result to a dataframe object to access it later.
- a dictionary is created with values for the columns which already exist in the target dataframe.
- It is appended to the target dataframe using the append() method.
dict = df = df.append(dict, ignore_index = True) df
Now, you’ve appended one row to the dataframe.
Dataframe Will Look Like
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
This is how you can insert a row to the dataframe using append.
Use this method when you want to add row to dataframe using dictionary or a pandas series.
Add row Using Concat
You can append a row to the dataframe using the concat() method. It concatenates two dataframe into one.
- List of dataframes – List of dataframes that needs to be concatenated
- ignore_index – Whether the index of the new dataframe should be ignored when concatenating to the target dataframe
- axis = 0 – To denote that rows of the dataframe need to be converted.
- It returns a new dataframe object which has the rows concatenated from two dataframes.
inplace concatenation is not supported. Hence, assign the result to a variable for later use.
df2 = pd.DataFrame() df = pd.concat([df, df2], ignore_index = True, axis = 0) df
- you’re creating a new dataframe with one row, and it is named df2 .
- You’re concatenating this to dataframe df which already has one row in it.
Both df and df2 will be concatenated and you’ll see two rows in the resultant dataframe.
Dataframe Will Look Like
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
1 | India | Kumar | Ram |
Add row Using iLOC
You can use the iLoc[] attribute to add a row at a specific position in the dataframe.
- iloc is an attribute for integer-based indexing used to select rows from the dataframe.
- You can also use it to assign new rows at that position.
Adding a row at a specific index position will replace the existing row at that position.
When you’re using iLoc to add a row,
- The dataframe must already have a row in the position. At least an empty row.
- If a row is not available, you’ll see an error IndexError: iloc cannot enlarge its target object . iLoc will not expand the size of the dataframe automatically.
In the following, a row is added at the index position 1 . It replaced the values available in that position with the new values.
df.iloc[1] = ['India', 'Shivam', 'Pandey'] df
Dataframe Will Look Like
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
1 | India | Shivam | Pandey |
This is how you can use the iloc[] to insert a row to the existing dataframe.
Use this method when you want to add rows at a specific position.
Add row Using LOC
loc[] attribute accesses a set of rows from the dataframe using the index label.
- Assign rows with a specific index label using the loc attribute.
- It’s not mandatory that a row already exists with a specific label. It’ll automatically extend the dataframe and add a row with that label, unlike the iloc[] method.
To demonstrate loc using the row indexes with names like a , b ,
- A new dataframe is created with labels a and b .
- A new row is assigned with the row label c using the loc[] method.
import pandas as pd # List of Tuples users = [ ('Shivam', 'Pandey', 'India'), ('Kumar', 'Ram' , 'India' ), ] #Create a DataFrame object df3 = pd.DataFrame( users, columns = ['First Name' , 'Last Name', 'Country'], index=['a', 'b']) print('Dataframe before adding a new row:\n') print('---------------------------------------\n') print(df3) df3.loc['c'] = ['Vikram', 'Aruchamy', 'India'] print('\nDataframe after adding a new row:\n') print('---------------------------------------\n') print(df3)
First a dataframe df3 is created with two rows with label a and b . Then a row is inserted with the label c using the loc[] method.
Dataframe Will Look Like
Dataframe before adding a new row: --------------------------------------- First Name Last Name Country a Shivam Pandey India b Kumar Ram India Dataframe after adding a new row: --------------------------------------- First Name Last Name Country a Shivam Pandey India b Kumar Ram India c Vikram Aruchamy India
This is how you can use the loc[] method to add rows to the dataframe. Either it is an empty dataframe, or it already has values.
Pandas Insert Empty Row
Empty rows can be appended by using the df.loc[df.shape[0]] and assigning None values for all the existing columns.
For example, if your dataframe has three columns,
df.loc[df.shape[0]] = [None, None, None] df
An empty row is added at the end of the dataframe.
Dataframe Will Look Like
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Raj | Kumar |
1 | India | Vikram | Aruchamy |
2 | India | Shivam | Pandey |
3 | India | Shivam | Pandey |
4 | India | Krishna | Kumar |
5 | None | None | None |
This is how you can add an empty row to the end of the dataframe.
Why You Should Not Add Rows One By One To Dataframe
You may need to create a dataframe and append one row at a time in various scenarios.
In that case, it is advisable to create a list first to hold all the records and create a dataframe with all the records from the list in one shot using the pd.DataFrame() method.
Calling the append() method for each row is a costlier operation. But adding the rows to the list is not costlier. Hence, you can add to the list and create a dataframe using that list.
data = [] data.append(['Krishna', 'Kumar', 'India']) data.append(['Ram', 'Kumar', 'India']) data.append(['Shivam', 'Pandey', 'India']) df = pd.DataFrame(data, columns=['First Name', 'Last Name', 'Country']) df
For more details about this scenario, refer StackOverflow answer.
Dataframe Will Look Like
First Name | Last Name | Country | |
---|---|---|---|
0 | Krishna | Kumar | India |
1 | Ram | Kumar | India |
2 | Shivam | Pandey | India |
This is how you can create a pandas dataframe by appending one row at a time.
Conclusion
To summarize, you’ve learned how to create empty dataframe in pandas and add rows to it using the append() , iloc[] , loc[] , concatenating two dataframes using concat() .
If you have any questions, comment below.