Appending Dataframes in Pandas with For Loops


Pandas is a powerful library in Python that offers an extensive list of operations that could be carried out with datasets. In this article, we would be exploring how to add new entities to an existing dataframe using a for loop. We shall make use of a set of strings that are readily available in a table format & add the additional information using each of the below techniques.

  • Appending dataframe with textual values
  • Appending dataframe with numerical values

You can append dataframes in Pandas using for loops for both textual and numerical values. For textual values, create a list of strings and iterate through the list, appending the desired string to each element. For numerical values, create a dataframe with specific ranges in each column, then use a for loop to add additional rows to the dataframe with calculated values based on the loop index.

Before getting started with any of these techniques one ought to kick things off by importing the pandas library using the below code.

import pandas as pd

Method I: Appending Dataframes with Textual Values

This technique shall deal with the dataframe containing textual values such as the one given below.

names_list = ['Stark', 'Banner', 'Rogers', 'Scott']

Yep! You’ve guessed it right. This section pays tribute to the original Avengers – Earth’s mightiest superheroes. In order to store the results let us create a blank list titled ‘Avengers’ as shown below.

Avengers = [ ]

It is within this list that the appended output churned through the for loop would be stored. Now comes the crucial part where the for loop would be used to iterate the list and append additional details to the existing values and return them as a result. So, we declare each element in the input names_list as a value and then specify the string that is to be added to that value as shown in the below code.

for value in names_list:
    dataframe_values = 'Mr. '+value

After the loop construction, it is time to turn our attention towards the output dataframe which contains the superhero names of each person given in the list.

df = pd.DataFrame(Avengers, columns=['Name'],index=['Ironman','Hulk','Captain','Antman'])

Finally, we can print the output values which were iterated through the for loop for adding ‘Mr.’ in front of all names.

print("Super heros:\n",df,"\n")
Output Dataframe For Printing The Results
Output Dataframe For Printing The Results
Dataframe Appended Using For Loop
Dataframe Appended Using For Loop

Method II: Appending Dataframes with Numerical Values

It would be properly unfair to touch only the textual aspect of the dataframe and toss the numerical data off to the wind. This method is about doing justice to the aforementioned statement by appending additional numbers to the already available entities of an input dataframe specified with ranges. All this shall be done with the aid of a for loop.

So, let us get started by constructing the input dataframe with a range of numbers from 2 to 8 in the first column, 12 to 18 in the second column and 22 to 28 in the third. Also, each column shall be defined as x,y and z as shown below.

data = pd.DataFrame({'x':range(2, 8),
'y':range(12, 18),
'z':range(22, 28)})
Input Dataframe Constructed
Input Dataframe Constructed

Let us now have a look at the output by using the print command.

Viewing The Input Dataframe
Viewing The Input Dataframe

It is evident from the above image that the result is a tabulation having 3 columns and 6 rows. Now let us deploy the for loop to include three more rows such that the output shall be in the form of 3×9. For these three additional rows, the rationale to be used is to multiply the value of each ‘i’ specified in the range by 2 as shown below.

for i in range(7, 10):
    data.loc[len(data)] = i * 2
For Loop Constructed To Append The Input Dataframe
For Loop Constructed To Append The Input Dataframe

Now view the final result using the print command and the three additional rows containing the multiplied values are returned.

Dataframe Appended With Three New Rows
Dataframe Appended With Three New Rows


Now that we have reached the end of this article, hope it has elaborated on the different techniques that can be used to append the dataframes from the Pandas library using the for loop. Here is another article that details how to extract a string between two substrings in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. 

As you continue exploring the capabilities of Pandas, think about how you might use these techniques to manipulate and analyze your own data. What other scenarios could benefit from appending dataframes in this way?