How to Add Rows to a DataFrame Pandas in Loop in Python [4 Methods]

Do you want to add rows to a dataframe in loop? In this Pandas article, I will explain how to add rows to a dataframe Pandas in loop in Python using different methods with some illustrative examples.

To add rows to a DataFrame in Pandas within a loop in Python, we can use several methods. The loc method allows direct assignment of values to specified row labels. The _append method (though not standard and generally not recommended) can be used for appending. Creating a list of dictionaries and then converting it to a DataFrame is efficient for batch processing. Finally, using concat with a list of Series is optimal for larger datasets, as it concatenates all Series at once after the loop.

Add Rows to a DataFrame Pandas in Loop in Python

There are four different methods to add rows to a dataframe Pandas in loop in Python:

  • using loc Method
  • using _append Method
  • Creating a List of Dictionaries
  • Using concat with a List of Series

Let’s see them one by one using some illustrative example:

1. Add rows to dataframe Pandas in loop using loc method

We can use the loc indexer to add a new row. This is straightforward but not the most efficient for large DataFrames.

READ:  NumPy unique function in Python [7 use cases]

Here is the code to add rows to a dataframe Pandas in loop in Python using the loc method:

import pandas as pd

weather_data = pd.DataFrame(columns=['City', 'Temperature', 'Humidity'])
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
temperatures = [59, 75, 52, 80, 85]
humidities = [63, 68, 72, 75, 20]

for i, city in enumerate(cities):
    weather_data.loc[i] = [city, temperatures[i], humidities[i]]
print(weather_data)

Output:

          City  Temperature  Humidity
0     New York           59        63
1  Los Angeles           75        68
2      Chicago           52        72
3      Houston           80        75
4      Phoenix           85        20

The output from the code executed in PyCharm can be seen in the screenshot provided below.

How to Add Rows to a DataFrame Pandas in Loop in Python

2. Pandas add rows to dataframe in loop using _append() function

The _append method can be used to add a single row or multiple rows. This method is more flexible but less efficient for very large DataFrames.

Here is the code to add rows to a dataframe Pandas in loop in Python using the _append() function:

import pandas as pd

population_data = pd.DataFrame(columns=['State', 'Population'])
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
populations = [39538223, 29145505, 21538187, 20201249, 13002700]
for i in range(len(states)):
    population_data = population_data._append({'State': states[i], 'Population': populations[i]}, ignore_index=True)
print(population_data)

Output:

          State Population
0    California   39538223
1         Texas   29145505
2       Florida   21538187
3      New York   20201249
4  Pennsylvania   13002700

The following screenshot illustrates the result after running the code in the PyCharm editor.

add data to dataframe in loop python

3. Python add rows to dataframe in loop by creating a list of dictionaries.

Instead of adding rows inside the loop, create a list of dictionaries where each dictionary represents a row, and then convert it into a DataFrame.

Here is the code to add rows to a dataframe Pandas in loop in Python by creating a list of dictionaries:

import pandas as pd

gdp_data_list = []
regions = ['Northeast', 'Midwest', 'South', 'West']
gdp_values = [8000, 7000, 9000, 8500]
for i in range(len(regions)):
    gdp_data_list.append({'Region': regions[i], 'GDP': gdp_values[i]})
gdp_data = pd.DataFrame(gdp_data_list)
print(gdp_data)

Output:

      Region   GDP
0  Northeast  8000
1    Midwest  7000
2      South  9000
3       West  8500

Below is a screenshot showing the output after the code was executed in the PyCharm editor.

pandas add row to dataframe in python

4. Python add row to dataframe in loop using concat with a list of series.

This method involves creating a list of series or dataframes and concatenating them at the end. It’s more efficient than the append function for larger datasets.

READ:  Pandas iterrows in Python [2 Examples]

Here is the code to add rows to a dataframe Pandas in loop in Python using the contact function with a list of series:

import pandas as pd

university_ranking_list = []
universities = ['MIT', 'Stanford', 'Harvard', 'Caltech', 'UChicago']
rankings = [1, 2, 3, 4, 5]
for i in range(len(universities)):
    university_ranking_list.append(pd.Series([universities[i], rankings[i]], index=['University', 'Ranking']))
university_rankings = pd.concat(university_ranking_list, axis=1).transpose()
print(university_rankings)

Output:

  University Ranking
0        MIT       1
1   Stanford       2
2    Harvard       3
3    Caltech       4
4   UChicago       5

The output of the code executed in Pycharm is illustrated in the screenshot below.

pandas add row to dataframe in loop in Python

Conclusion

Here, I have explained four different methods to add rows to a dataframe Pandas in loop in Python. These include using the loc method for direct assignment, the unconventional _append method, efficiently creating a dataframe from a list of dictionaries, and the concat method with a list of series for handling larger datasets.

Each method serves different use cases and performance considerations in data manipulation tasks.

You may also like to read: