3 Ways to Append Rows to Pandas DataFrames

Learn a simple way to append rows in the form of arrays, dictionaries, series, and dataframes to another dataframe.



3 Ways to Append Rows to Pandas DataFrames
Image by Author
 

In this mini tutorial, we will learn three ways to append rows to pandas dataframe. We will also learn about the most effective and easy ways to add multiple rows.

 

Method 1

 

We will use pandas DataFrame() and input data in the form of a dictionary to create a sample dataframe for the students enrolled in the online master’s degree.

import pandas as pd

data1 = pd.DataFrame(
    {
        "ID": [15, 16, 17, 18, 19],
        "Name": ["Abid", "Matthew", "Nisha", "Natassha", "Nahla"],
        "CGPA": [2.3, 3.0, 3.9, 2.5, 3.2],
        "Dept": ["EEE", "IT", "CS", "BA", "LAW"],
        "Region": ["Islamabad", "Ontario", "London", "Saba", "Denver"],
    }
)
data1

 

We have five columns and five distinct rows. It will be the base dataframe.

 

3 Ways to Append Rows to Pandas DataFrames
 

We can append rows in the form of pandas Series.

To add a Series to the dataframe, we will use the append() function after the dataframe object and add the series object in the bracket. The ignore_index is set to True so that the resulting index will be labeled as 0,1,....,n-1

row1 = pd.Series([25, 'Franc', 3.3, 'CS', 'Paris'], index=data1.columns)
data1 = data1.append(row1,ignore_index=True) 
data1

 

As we can observe, we have successfully added a student’s information into the dataframe.

 

3 Ways to Append Rows to Pandas DataFrames
 

Similarly, we can also append a dataframe. In our case, we have created a data2 dataframe and used the .append() function to add multiple rows to data1.

data2 = pd.DataFrame(
    {
        "ID": [78, 88, 98],
        "Name": ["Nick", "Stan", "Ludwig"],
        "CGPA": [2.3, 2.5, 3.2],
        "Dept": ["EEE", "BA", "LAW"],
        "Region": ["Puerto Rico", "Miami", "Malmo"],
    }
)

 

To view the last five rows, we have used .tail().

 

3 Ways to Append Rows to Pandas DataFrames
 

Method 2

 

The second method is pretty much straightforward. We can create and append a dictionary to the data frame using append. Make sure the dictionary is following the format below. Every record should have a column name with the values.

row2 = {
    "ID": 105,
    "Name": "Nana",
    "CGPA": 3.1,
    "Dept": "IT",
    "Region": "Tokyo",
}
data1 = data1.append(row2, ignore_index=True)
data1.tail()

 

3 Ways to Append Rows to Pandas DataFrames
 

Method 3

 

The third method is an effective way of appending rows to the dataframe.

Note: DataFrame.append() or Series.append() has been depreciated since the version 1.4.0. So, if you want to use the latest version, you need to use this method.

To concat two dataframe or series, we will use the pandas concat() function. It provides advanced features such as appending columns using an inner or outer join.

In our case, we have created a third dataframe data3 using an array. We can also append a Numpy array to the dataframe, but we need to convert it into a dataframe first.

We are concatenating data1 and data3 along the 0 axis. It means we are appending rows, not columns.

data3 = pd.DataFrame(
    [[126, "Floki",2.5,"BA","Copenhagen" ],
    [188, "Lee",2.5,"LAW", "Miami"]],
    columns= data1.columns
)

 

As we can the, we have successfully added rows using the concat function.

 

3 Ways to Append Rows to Pandas DataFrames
 

Conclusion

 

You can also use .loc[<index_number>] to add rows at the end of the dataframe.

For example:

data1.loc[12] = [200, "Bala",2.4,"DS","Delhi"]

 

It is a simple way, but not adequate as you have to keep track of the index number.

In the tutorial, we have learned various ways to add rows to pandas dataframe. Learning to add data, manage it, and process it for analytical tasks is the first step toward becoming a professional data scientist.

Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master's degree in Technology Management and a bachelor's degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.