How to append output of a for loop in a dictionary?

How to append output of a for loop in a dictionary?

How to append output of a for loop in a dictionary?

This recipe helps you append output of a for loop in a dictionary


Recipe Objective

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

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

Step 1 - Setup the Data

df= {'Table of 9': [9,18,27], 'Table of 10': [10,20,30]}

Let us create a simple dataset of tables.

Step 2 - Appending the dictionary

for i in range(4,11): df['Table of 9'].append(i*9) df['Table of 10'].append(i*1)

Append function on a particular list of dictionary helps in appending values. Here, for previously assigned list, we are appending values

Step 3 - Printing results


Simply use print function to print df

Step 4 - 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.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.