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

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

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.

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.

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.

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.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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.