Pandas Insert Row

“A Python framework for working with data frames is called Pandas. It may be used for a variety of purposes, including reading, exporting CSV files, and converting NumPy arrays to dataFrames. Three parameters make up the Pandas dataFrame. DataFrames are quite helpful since they offer a simple way to print a table view and then modify it as needed. You might need to establish a new data frame and add rows selectively when using the dataFrame for data analysis to generate a data frame with particular records.

Unless you assign names to each row, the index will be the integer starting at zero for the associated row. Additionally, it is simple to refer to a column by its name, such as “Data,” or by its location within the DataFrame.”

Pandas Adding Row

Use the loc[] function for adding any row or its relevant column to a DataFrame. The insert() function or behaving as if you were adding a slice of a DataFrame and placing the column name within square brackets “[]” are the two additional ways to add rows and columns. A row can only be added to the bottom of a DataFrame using the”loc[]” function to add it to the DataFrame. The information in that row will be replaced with the data you are inserting if you specify any other index in the DataFrame. The “append()” method passes the new DataFrame within the “[]” and applies the dot operator with the old DataFrame. At the end point of the relevant row, it will add a new row.

You must be aware of the existing columns in the data frame to add a new row. There are four ways to add rows to the DataFrame “append()”, “concat()”, “iloc[]” and “.loc[]”. We used two techniques in our examples.

The Syntax for Pandas Inserting Row by .loc[] method

The syntax for adding a row using the “.loc[]” method is as above

The Syntax for Pandas Inserting Row by Append() Method

Example 1:  Adding Row by Using .loc[] Method

Starting with the first example, we add a row single row to our data frame using the “df.loc()” method. When a row is inserted into a Pandas DataFrame, it is added to the end of the DataFrame while the original rows remain stable. Utilizing “DataFrame.loc[]” we can insert the row at the end of our Dataframe. Examples include inserting a python list “cars” as a row into a pandas DataFrame, which effectively adds a row to the DataFrame with contents provided by a list.

In this example, we built data frames termed “cars”. There are three columns “Name” is the title of the first column. In the list of columns “Name”, we have the names of some cars, “Ford”, “Honda,” and “Toyota”. Price is the second column that we have, and it holds the values “3000000”, “420000,” and “400000” the third column we have is “Model,” and it contains the values “2021”, “2018,” and “2019”. Now, we’re going to display our data frame using “display(df)”.

The data frame will now feature a new column utilizing the “df.loc()” function. By using “df.loc()”, we simply add a single row. The list we included in our last row is “Volkswagen”, “250000,” and “2010”. Now it’s simple to add these values to the last row of our dataFrame. To figure out where to place the new row, we can use “len(Df.index”) to find the number of rows. The index, which behaves like an address, can be used to retrieve any data point within the data frame or series. Both rows and columns have indexes “rows” indexes are known as such, whereas “columns” indexes are known as general column names. Finally, “display(df)” will show a new data frame with a new row.

By pressing the “Run file” button on the “Spyder” tool’s interface or by pressing “Shift+Enter,” you can run the Python program indicated above. The Spyder interface will then display two dataFrames with three columns as a result. In this program, we first generate a DataFrame with three columns, “Name”, “Price,” and “Model,” having some values in it by using “pd.DataFrame” and displaying this on screens by using “display(df)” after that, we added a new “row” in DataFrame containing with the list “Volkswagen”, “250000” and “2010”.

A new “row” will be added by using the “df.loc()” function, and the “len(df.index)” displays where the row would be placed in the DataFrame. The output now clearly displayed the DataFrame with a new row.

Example 2:  Adding Row by Using the append() Method

The append() feature adds rows from some other DataFrame to the give up of the one that has been supplied, returning a new DataFrame object in the process. The authentic DataFrames missing columns are introduced as new columns, and the new cells are packed with NaN values. The original DataFrame is left unchanged only a new DataFrame object is returned by the “append()” method.

This sample will determine how to utilize the “df.append()” method to insert a new row into a DataFrame. The first step is to build a DataFrame with two columns. Our DataFrame’s name, in this example, is “data,” and the column we have chosen from it is “Country” and “Capital”. A list of values is kept in these columns. In the first column “Country” we have “England”, “Australia” and “Turkey” and in the second column “Capital” we have the list of their capital countries in which we have “London”, “Canberra” and “Istanbul “.

We now add a new “row” to our data frame using the “df.append()” method. The newly added values for a row are “country: Pakistan” and “Capital: Islamabad” this indicates that “Pakistan” will be printed as in the column “country” and “Islamabad” is printed as in the column “Capital”. Use “ignore_ index=True” to confirm that your index is clean. “ignore_index=True” means the original indexes, in this case, are disregarded and replaced by “0”, “1”, “2,” and “3”. A piece of data will be added by using pandas “append()” method to another DataFrame. This entails combining “d1” and “d2” to get “d1+d2”. This is comparable to a standard Python “Append”. Now by using “print(df)”. We will now display our data frame using “print(df)”.

The two dataFrames are displayed in this output image. We have the columns “Country” and “Capital” in the data frame. It includes a list of some data. Then, we can see that the output image displays this data frame shown inside it. We expanded the data frame by adding a new row using the “append()” technique. We can see that our new row has been generated in the data frame and is shown in the image with an “index” from 0 to 3.


A DataFrame may sometimes additionally require the addition of new rows. In this article, methodologies are used to add rows to a DataFrame. If we have a look at the first example, a data frame was made using “pd.dataframe”. Three columns make up this data frame, which is titled “Car”. We use the “.loc[]” function to add a row to this DataFrame, and “print(df)” is used to display the DataFrame on the screen. In the same way, we used the “append()” method to add a row to the DataFrame in our second example. These two techniques are useful for adding rows to a DataFrame.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.