Data


In [124]:
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

def cat_split(n):
    def inner(x):
        hierarchy = x.split('>')
        hierarchy = hierarchy + [""]*(n - len(hierarchy))
        return tuple(hierarchy)
    return inner

In [125]:
problems = pd.read_csv('problems.csv', sep=', ')
problems['Code'] = problems['Code'].apply(cat_split(3))
problems


Out[125]:
Problem Pain People Code
0 Uses pact 1 johnf (accountability, uses-product, )
1 Uses fitbit 2 johnf (tracking, uses-product, )
2 Has read "The Power of Habit" 1 jake (challenges, passive, reading)
3 Shares with his wife 1 jake (accountability, wife, )
4 Asked GF to ring in the morning 2 johnf (accountability, wife, )

In [115]:
people = pd.read_csv('people.csv', sep=', ')
people


Out[115]:
People First Name Last Name Age
0 johnf John Fawcett 38
1 jake Jake Desyllas 43

In [126]:
demographics = pd.read_csv('demographics.csv', sep=', ')
demographics['Demographic'] = demographics['Demographic'].apply(cat_split(3))
demographics


Out[126]:
Demographic People
0 (job, entrepreneur, ) johnf
1 (job, creative, blogger) johnf
2 (job, creative, writer) johnf
3 (habit, diet, ) johnf
4 (habit, exercise, ) johnf
5 (job, coach, ) jake
6 (job, entrepreneur, ) jake
7 (habit, writing, ) jake
8 (habit, wakeing-up, ) jake

Demographics by problem


In [148]:
prob_demo = pd.merge(problems, demographics, on='People')
prob_demo.stack()
prob_demo.unstack(1)


Out[148]:
Problem      0                           Uses pact
             1                           Uses pact
             2                           Uses pact
             3                           Uses pact
             4                           Uses pact
             5                         Uses fitbit
             6                         Uses fitbit
             7                         Uses fitbit
             8                         Uses fitbit
             9                         Uses fitbit
             10    Asked GF to ring in the morning
             11    Asked GF to ring in the morning
             12    Asked GF to ring in the morning
             13    Asked GF to ring in the morning
             14    Asked GF to ring in the morning
             15      Has read "The Power of Habit"
             16      Has read "The Power of Habit"
             17      Has read "The Power of Habit"
             18      Has read "The Power of Habit"
             19               Shares with his wife
             20               Shares with his wife
             21               Shares with his wife
             22               Shares with his wife
Pain         0                                   1
             1                                   1
             2                                   1
             3                                   1
             4                                   1
             5                                   2
             6                                   2
                                ...               
Code         16     (challenges, passive, reading)
             17     (challenges, passive, reading)
             18     (challenges, passive, reading)
             19           (accountability, wife, )
             20           (accountability, wife, )
             21           (accountability, wife, )
             22           (accountability, wife, )
Demographic  0               (job, entrepreneur, )
             1            (job, creative, blogger)
             2             (job, creative, writer)
             3                     (habit, diet, )
             4                 (habit, exercise, )
             5               (job, entrepreneur, )
             6            (job, creative, blogger)
             7             (job, creative, writer)
             8                     (habit, diet, )
             9                 (habit, exercise, )
             10              (job, entrepreneur, )
             11           (job, creative, blogger)
             12            (job, creative, writer)
             13                    (habit, diet, )
             14                (habit, exercise, )
             15                     (job, coach, )
             16              (job, entrepreneur, )
             17                 (habit, writing, )
             18              (habit, wakeing-up, )
             19                     (job, coach, )
             20              (job, entrepreneur, )
             21                 (habit, writing, )
             22              (habit, wakeing-up, )
dtype: object

In [146]:
prob_demo_pivot = prob_demo.pivot_table(index='Code', columns='Demographic')
prob_demo_pivot.index = pd.MultiIndex.from_tuples(prob_demo_pivot.index)
prob_demo_pivot


Out[146]:
Pain
Demographic (habit, diet, ) (habit, exercise, ) (habit, wakeing-up, ) (habit, writing, ) (job, coach, ) (job, creative, blogger) (job, creative, writer) (job, entrepreneur, )
accountability uses-product 1 1 NaN NaN NaN 1 1 1.0
wife 2 2 1 1 1 2 2 1.5
challenges passive reading NaN NaN 1 1 1 NaN NaN 1.0
tracking uses-product 2 2 NaN NaN NaN 2 2 2.0

In [ ]:


In [ ]: