<jupyter_start><jupyter_text>Conjoint Analysis Calculates utilities, price units, and perceived values for each product/service from product/services part-worths from a conjoint analysis data file (JSON), outputs Excel tables.Example analysis estimates customers' perceived value (willingness to pay) for Trailblazers game packages.<NAME> 2017<jupyter_code>import pandas as pd
from IPython.display import display
import numpy as np

save_as = 'trailblazers'
file_path = 'trailblazers.csv'
df = pd.read_csv(file_path)

display(df.head())<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def get_diff(a):
    return max(a) - min(a)


def get_attr_importance(pw_diff, total_pw):
    return pw_diff / total_pw * 100


def get_attr_util(df):
    df = df.copy()
    price_unit = None
    
    # calc the part-worth difference from all levels, max - min
    partworths = [r for r in df['Partworth']]
    _min = min(partworths)
    prices = [r for r in df['Price']]
    pw_diff = get_diff( partworths )
    
    # calc price utility of each level
    util = lambda pw: (pw - _min) if (pw != _min) else pw
    utilities = [util(pw) for pw in partworths]
    df['Utility'] = utilities
    
    if not np.isnan(prices[0]):
        price_diff = get_diff(prices)
        price_unit = price_diff / pw_diff
        
    df['PriceUnit'] = [price_unit for r in prices]
    
    return [df, pw_diff]


def get_attr_values(df, price_unit):
    """
    Return dataframe with calculated perceived values 
    from part-worths and utilities. 
    """
    perc_values = []   
    get_perc_val = lambda u: u * price_unit
    perc_values = [get_perc_val(u) for u in df['Utility']]
    return perc_values

def get_wtp(df):
    price_unit = None
    pw_total = 0.00
    attrs_list = []
    imp_data = []
    
    attrs = df.groupby(['Attribute'])
    attr_names = [a[0] for a in attrs]
    
    for attr in attrs:
        attr_df, pw_diff = get_attr_util(attr[1])
        pw_total += pw_diff
        attrs_list.append(attr_df)
        #display(attr_df.head())
        
        if 'PriceUnit' in attr_df.columns:
            price_unit = attr_df['PriceUnit'].values[0]
                 
    for attr, name in zip(attrs_list, attr_names):
        attr['WTP'] = get_attr_values(attr, price_unit)
        u_diff = get_diff( [u for u in attr['Utility'].values] )
        imp_data.append( (name, u_diff, get_attr_importance(u_diff, pw_total) ) )
    
    df_imp = pd.DataFrame(data=imp_data, columns=['Attribute', 'Utility', 'Importance'])\
                      .sort_values(by='Importance', ascending=False)
        
    return [attrs_list, df_imp]<jupyter_output><empty_output><jupyter_text>Import data and calculate WTP.<jupyter_code>attrs, imp = get_wtp(df)

imp.to_csv(save_as+'_importance.csv')
display(imp)

merged = pd.concat(attrs).sort_index()
merged.to_csv(save_as+'_values.csv')
display(merged)

pivot = merged.T
pivot.to_csv(save_as+'_pivot.csv')
display(pivot)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11040481766477082, 'fraction_numerical': 0.00468384074941452, 'mean_word_length': 3.358600583090379, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '104117', 'license': 'MIT', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1038, 'n_words': 247, 'path': 'trailblazers_wtp.ipynb', 'repo_name': 'jbonfardeci/conjoint-analysis', 'size': '2998'}