Adding a row in Pandas

In this article, you will discover how to add (or insert) a row into a Pandas DataFrame. You’ll discover how to add one row, or several rows, and at particular locations. A list, a series, and a dictionary are other alternatives to adding a row.

After finishing this tutorial, you will know the following:

  • Various approaches of adding a single row and numerous rows to a Pandas DataFrame
  • How to add a row to a Pandas DataFrame at a certain location, such as the top or bottom
  • How to add rows using dictionaries, lists, and the Pandas Series

Adding a row in Pandas

For adding any row or its pertinent column to a DataFrame, use the loc[] function. There are two other ways to add rows and columns: using the insert() function or acting like you’re adding a slice of a DataFrame and enclosing the column name in square brackets “[]”. Only the “loc[]” function can add a row to a DataFrame, and only at the bottom. If you give any other index in the DataFrame, the data you insert will replace the information in that row. The “append()” method uses the dot operator to the old DataFrame while passing the new DataFrame inside the “[]”. Consequently, a new row is added at the relevant row’s endpoint.

To add a new row, you must be aware of the existing columns in the data frame. The DataFrame can be expanded in four different ways: “append(),” “concat,” “iloc,” and “.loc.” In our demonstrations, we used two strategies.

The syntax for using the .loc[] function to insert rows in Pandas

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>pandas.DataFrame.loc[]
</pre>
</div>

The “.loc[]” method’s syntax for inserting a row is as stated above. The syntax for the Row by Append() method in Pandas is as shown below.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>dataframe = dataframe.append(new_row, ignore_index=True)</pre>
</div>

How to load a Sample Pandas DataFrame

You can copy the code below into your preferred code editor and follow this instruction line after line. Feel free to use your data if you have it (though your results will undoubtedly vary):

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#A Sample Pandas DataFrame is Loaded
import pandas as pd

employee_df = pd.DataFrame.from_dict({
    'Name': ['Bright', 'Joy', 'Green', 'White'],
    'Age': [39, 17, 23, 53],
    'Residence': ['New York', 'Liverpool', 'London', 'Paris']
})

print(employee_df )</pre>
</div>

Three columns and four records cover each person’s Name, Age, and Residence.

Row Addition in a Pandas DataFrame

The Pandas.append() method is the simplest way to add or insert a new row into a Pandas DataFrame. The Pandas concat() function has a helper method called .append(). Read along to find out more about how these features operate. You’ll discover three distinct approaches in this section for adding a single row to a Pandas DataFrame.

Making Use of a Dictionary to Add a Row to a Pandas DataFrame

Say we wish to create a new row using the information below:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>{'Name':'Mike', 'Age':32, 'Residence':'Barcelona'}

employee_df  = employee_df .append({'Name':'Mike', 'Age':32, 'Location':'Barcelona'}, ignore_index=True)

print(employee_df )</pre>
</div>

In the example mentioned above, we demonstrated how to use a dictionary in the addition of a new row to a DataFrame. We had to pass the ignore_index=True parameter because we passed a dictionary.

Using a List, Add a Row to a Pandas DataFrame

Since we can’t just use the .append() function to add a list to a Pandas DataFrame, the process is a little different. We need to employ the loc accessor to accomplish this. The length of the DataFrame will serve as the label for our loc accessor. It will produce a new row, as demonstrated below:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>employee_df.loc[len(employee_df)] = ['Mike', 32, 'Barcelona']
print(employee_df)</pre>
</div>

Using iloc is more difficult since it requires that the index position already be filled in. As a result, we would either need to add an empty row first or replace data.

Using a Series, Add a Row to a Pandas DataFrame

Try adding the same row as in the example above using a Pandas Series that we may make using a Python list. We only pass a list to the Series() method to turn a list into a series. Let’s check how this functions:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># Inserting a novel Row into a Series
employee_df = employee_df.append(pd.Series(['Mike', 32, 'Barcelona']), ignore_index=True)
print(employee_df)</pre>
</div>

Add a Row to the Top of a Pandas DataFrame

Reversing the choices you learned about previously will easily add a row to the top of a Pandas DataFrame. It means that we add the bigger DataFrame to the new row. But first, we have to make a DataFrame. The class pd.DataFrame() can be used to accomplish this. Let’s look at this:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>employee_df  = employee_df .DataFrame([['Mike', 32, 'Barcelona']], columns=df.columns).append(employee_df )
print(employee_df )
</pre>
</div>

Inserting a Row at a Specific Index in a Pandas DataFrame

A little bit different is adding a row at a particular index. We must employ the loc accessor, as seen in the example of using lists. However, adding a row at a specific index will replace this. Instead, we can provide a value near where the new row should be inserted. If we wish to insert a new row at index 2, for instance, and we currently have indices from 0 to 3, we may assign it using index 1.5. Let’s check how this functions:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># Inserting a Row at a Specific Index
employee_df.loc[1.5] = ['Mike', 32, 'Barcelona']
employee_df = employee_df.sort_index().reset_index(drop=True)

print(employee_df)</pre>
</div>

It naturally makes the following assumptions:

  • Your index is initially at 0. If not, change your loc index accordingly.
  • Such a mutation of your index is possible. This might not be the case if your index has more significance.

Add Several Rows to a Pandas DataFrame

The technique for adding more than one row to a Pandas DataFrame is the same as adding a single row. However, it might be considerably faster since we can pass in all the objects at once. For instance, adding items that require a dictionary is as simple as adding a list of dictionaries.

Let’s look at an illustration:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># Row expansion in a Pandas DataFrame
employee_rows = [{'Name': 'Mike', 'Age': 32, 'Residence': 'Barcelona'}, {'Name': 'Ann', 'Age': 63, 'Residence':'CapeTown'}]

employee_df = employee_df.append(employee_rows, ignore_index=True)

print(employee_df)
</pre>
</div>

Example: Using the append() Method to Add a Row

By adding rows from another DataFrame to the one supplied, the append() function creates a new DataFrame object. The missing columns from the original DataFrames are added as new columns, and the new cells are stuffed with NaN values. Only a new DataFrame object returned by the “append()” method is added, leaving the existing DataFrame object unaltered.

This example shows how to add a new row to a DataFrame using the “df.append()” method. Creating a DataFrame with two columns is the first step. In this case, the name of our DataFrame is “data,” and the columns we have selected from it are “Employee” and “Station.” These columns maintain a list of values. Joy, Green, and Bright are listed in the first column labeled “Employee,” and London, New York, and Paris are included in the second column labeled “Station.”

Now, using the “df.append()” method, we expand our data frame by adding a new “row.” “Employee: Cate” and “Station: Cairo” are the two newly added values for a row, denoting that “Cate” would be printed in the “Employee” column and “Cairo” in the “Residence” column, respectively. To ensure your index is clean, use the command “ignore_ index=True.”

“ignore index=True” instructs the computer to ignore the original indexes and use “0,” “1,” “2,” and “3” instead. The pandas “append()” method will be used to add a piece of data to another DataFrame. To get “d1+d2,” “d1” and “d2” must be combined.The latter is similar to a typical Python “Append”. Using “print(df),” we will now display our data frame.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre># begin by importing Pandas library
import pandas as pd
# subsequently import NumPy
import numpy as np

emp_data ={'Employee':['Joy', 'Green','Bright'],
	'Station':['London','New York','Paris']}
	
emp_df = pd.DataFrame(emp_data)
print(emp_df)

new_df ={'Employee':'Cate','Station':'Cairo'}
df = new_df .append(new_df , ignore_index=True)
print(df)</pre>
</div>

This output displays the two dataFrames. The data frame contains the columns “Employee” and “Station.” There is a list of some data included. The output then shows this data frame with its contents inside, as can be seen. Using the “append()” method, we added a new row to the data frame to make it larger. As observed, the data frame has generated our new row, represented in the image with an “index” ranging from 0 to 3.

Conclusion

Pandas is a Python framework for working with data frames. It may be used to read data, export CSV files, and change NumPy arrays into dataFrames, among other things. The Pandas dataFrame consists of three parameters. Since they provide a quick way to print a table view and then alter it as necessary, DataFrames are quite useful. When utilizing the dataFrame for data analysis to create a data frame with specific records, you might need to create a new data frame and add rows carefully.

The index will be the integer beginning at zero for the related row unless you give each row a name. A column’s name, such as “Data,” or its location within the DataFrame can also be used to refer to it.

You discovered how to insert and add rows into a Pandas DataFrame in this session. To accomplish this, you acquired various techniques, including employing dictionaries, lists, and the Pandas Series. Additionally, you learned how to add new rows at the top, bottom, and a certain index. Finally, you discovered how to add several rows to a Pandas DataFrame simultaneously.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *