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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

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

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS