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.
Code
dict = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
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.
Table of Contents
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.
Code
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.
Parameters
dictionary
orPandas Series
orDataframe
– Object with values for new rowignore_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.
Returns
- A resultant dataframe which has the rows from the target dataframe and a new row appended.
inplace
append is not possible. Hence, do not forget to assign the result to a dataframe object to access it later.
In the following example,
- 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 = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
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.
To add one row,
- Create a dataframe with one row
- Concatenate it to the existing dataframe.
Parameters
It accepts,
List of dataframes
– List of dataframes that needs to be concatenatedignore_index
– Whether the index of the new dataframe should be ignored when concatenating to the target dataframeaxis = 0
– To denote that rows of the dataframe need to be converted.
Returns
- 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.
Snippet
df2 = pd.DataFrame({'First Name': ['Kumar'],
'Last Name' : ['Ram'],
'Country' : ['India']})
df = pd.concat([df, df2], ignore_index = True, axis = 0)
df
In the above example,
- 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.
Code
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.
Code
To demonstrate loc using the row indexes with names like a
, b
,
- A new dataframe is created with labels
a
andb
. - A new row is assigned with the row label
c
using theloc[]
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.
Code
For example, if your dataframe has three columns,
- Create a series with 3
None
values - Assign it to the last position of the dataframe.
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.
Code
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.