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.
import pandas as pd
We have imported pandas which is needed.
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)
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