How to create crosstabs from a Dictionary in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to create crosstabs from a Dictionary in Python?

How to create crosstabs from a Dictionary in Python?

This recipe helps you create crosstabs from a Dictionary in Python

0
In [1]:
## How to create crosstabs from a Dictionary in Python
def Kickstarter_Example_83():
    print()
    print(format('How to create crosstabs from a Dictionary in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd
    raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
                'company': ['infantry', 'infantry', 'cavalry', 'cavalry', 'infantry', 'infantry', 'cavalry', 'cavalry','infantry', 'infantry', 'cavalry', 'cavalry'],
                'experience': ['veteran', 'rookie', 'veteran', 'rookie', 'veteran', 'rookie', 'veteran', 'rookie','veteran', 'rookie', 'veteran', 'rookie'],
                'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
                'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
                'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}

    df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'experience', 'name', 'preTestScore', 'postTestScore'])
    print(); print(df)

    # Create a crosstab table by company and regiment
    df1 = pd.crosstab(df.regiment, df.company, margins=True)
    print(); print(df1)

    # Create more crosstabs
    df2 = pd.crosstab([df.company, df.experience], df.regiment,  margins=True)
    print(); print(df2)

    df3 = pd.crosstab([df.company, df.experience, df.preTestScore], df.regiment,  margins=True)
    print(); print(df3)

Kickstarter_Example_83()
***************How to create crosstabs from a Dictionary in Python****************

      regiment   company experience      name  preTestScore  postTestScore
0   Nighthawks  infantry    veteran    Miller             4             25
1   Nighthawks  infantry     rookie  Jacobson            24             94
2   Nighthawks   cavalry    veteran       Ali            31             57
3   Nighthawks   cavalry     rookie    Milner             2             62
4     Dragoons  infantry    veteran     Cooze             3             70
5     Dragoons  infantry     rookie     Jacon             4             25
6     Dragoons   cavalry    veteran    Ryaner            24             94
7     Dragoons   cavalry     rookie      Sone            31             57
8       Scouts  infantry    veteran     Sloan             2             62
9       Scouts  infantry     rookie     Piger             3             70
10      Scouts   cavalry    veteran     Riani             2             62
11      Scouts   cavalry     rookie       Ali             3             70

company     cavalry  infantry  All
regiment
Dragoons          2         2    4
Nighthawks        2         2    4
Scouts            2         2    4
All               6         6   12

regiment             Dragoons  Nighthawks  Scouts  All
company  experience
cavalry  rookie             1           1       1    3
         veteran            1           1       1    3
infantry rookie             1           1       1    3
         veteran            1           1       1    3
All                         4           4       4   12

regiment                          Dragoons  Nighthawks  Scouts  All
company  experience preTestScore
cavalry  rookie     2                    0           1       0    1
                    3                    0           0       1    1
                    31                   1           0       0    1
         veteran    2                    0           0       1    1
                    24                   1           0       0    1
                    31                   0           1       0    1
infantry rookie     3                    0           0       1    1
                    4                    1           0       0    1
                    24                   0           1       0    1
         veteran    2                    0           0       1    1
                    3                    1           0       0    1
                    4                    0           1       0    1
All                                      4           4       4   12

Relevant Projects

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

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.

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.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.