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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.