Search code examples
pythonpython-3.xdataframefeature-extraction

Calculating Churn Rate for per model ID


I want to divide the Churn Flag column into Y and N columns to calculate the Churn rate per model id easily. There are total 365 unique model id. So i want to keep only the model with churn rate grater than 80% because it's not easy to do it manually so i want to do it via programme. But to do it first I have to divide the churn flag as Y and N.

Sample of the dataframe:-

enter image description here


Solution

  • Edit: This will keep models with churn rate > 80%

    import pandas as pd
    
    df = pd.pivot_table(df, index='model id', columns='churn flag', aggfunc='sum', fill_value=0).reset_index()
    df.columns=['model id', 'N', 'Y']
    df['churn rate'] = df['Y'] / (df['N'] + df['Y'])
    df = df[df['churn rate']>0.8]