How to append output of a for loop in a python dataframe?

How to append output of a for loop in a python dataframe?

How to append output of a for loop in a python dataframe?

This recipe helps you append output of a for loop in a python dataframe


Recipe Objective

In python, while operating on list, we might need to store each loop output in a dataframe with each iteration.

So this recipe is a short example on how to append output of for loop in a pandas dataframe. Let's get started.

Step 1 - Import the library

import pandas as pd

Let's pause and look at these imports. Pandas is generally used for data manipulation and analysis.

Step 2 - Setup the Data

df= pd.DataFrame({'Table of 9': [9,18,27], 'Table of 10': [10,20,30]})

Let us create a dataframe containing some tables of 9 and 10.

Step 3 - Appending dataframe in a for loop

for i in range(4,11): df=df.append({'Table of 9':i*9,'Table of 10':i*10},ignore_index=True)

Comparing to append function in list, it applies a bit different for dataframe. As soon as any dataframe gets appnended using append function, it is note reflected in original dataframe. To store the appended information in a dataframe, we again assign it back to original dataframe.

Step 4 - Printing results


Simply use print function to print new appended dataframe.

Step 5 - Let's look at our dataset now

Once we run the above code snippet, we will see:

Scroll down to the ipython notebook below to see the output.

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.