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

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

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.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.