How to create crosstabs from a Dictionary in Python?

This recipe helps you create crosstabs from a Dictionary in Python

Recipe Objective

Have you ever tried to do a cross features analysis for that you need to create crosstabs basis on different features.

So this is the recipe on we can create crosstabs from a Dictionary in Python.

Master the Art of Data Cleaning in Machine Learning

Step 1 - Import the library

import pandas as pd

We have imported pandas which is needed.

Step 2 - Setting up the Data

We have created a dataset by making a dictionary with features and passing it through the dataframe function. raw_data = {"first_name": ["Sheldon", "Raj", "Leonard", "Howard", "Amy"], "last_name": ["Copper", "Koothrappali", "Hofstadter", "Wolowitz", "Fowler"], "age": [42, 38, 36, 41, 35], "Comedy_Score": [9, 7, 8, 8, 5], "Rating_Score": [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ["first_name", "last_name", "age", "Comedy_Score", "Rating_Score"]) print(df)

Step 3 - Making CrossTab Table

For better understanding we are making different datasets with different number of features for crosstab table. First we have created for one feature that is first_name then in next for two and then for three features. df1 = pd.crosstab(df.first_name, df.age, margins=True) print(df1) df2 = pd.crosstab([df.age, df.Comedy_Score], df.first_name, margins=True) print(df2) df3 = pd.crosstab([df.age, df.Comedy_Score, df.Rating_Score], df.first_name, margins=True) print(df3) So the output comes as

  first_name     last_name  age  Comedy_Score  Rating_Score
0    Sheldon        Copper   42             9            25
1        Raj  Koothrappali   38             7            25
2    Leonard    Hofstadter   36             8            49
3     Howard      Wolowitz   41             8            62
4        Amy        Fowler   35             5            70

age         35  36  38  41  42  All
first_name                         
Amy          1   0   0   0   0    1
Howard       0   0   0   1   0    1
Leonard      0   1   0   0   0    1
Raj          0   0   1   0   0    1
Sheldon      0   0   0   0   1    1
All          1   1   1   1   1    5

first_name        Amy  Howard  Leonard  Raj  Sheldon  All
age Comedy_Score                                         
35  5               1       0        0    0        0    1
36  8               0       0        1    0        0    1
38  7               0       0        0    1        0    1
41  8               0       1        0    0        0    1
42  9               0       0        0    0        1    1
All                 1       1        1    1        1    5

first_name                     Amy  Howard  Leonard  Raj  Sheldon  All
age Comedy_Score Rating_Score                                         
35  5            70              1       0        0    0        0    1
36  8            49              0       0        1    0        0    1
38  7            25              0       0        0    1        0    1
41  8            62              0       1        0    0        0    1
42  9            25              0       0        0    0        1    1
All                              1       1        1    1        1    5

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

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.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.