How to calculate quarterly wise churn and retention rate using python

2024/10/5 14:53:50

How to calculate quarterly wise churn and retention rate with date column using python. with date column i want to group that quarterly using python.

This is used to calculate the churn count groupby quarterly

quarterly_churn_yes = out.loc[out['Churn'] == 'Yes'].groupby(out["Date"].dt.quarter).count()
print(quarterly_churn_yes["Churn"])Date
1    1154
2     114
3      68
4      69
Name: Churn, dtype: int64

This is used to calculate the churn rate groupby quarterly

total_churn = out['Churn'].count()
print(total_churn) quarterly_churn_rate = out.groupby(out["Date"].dt.quarter).apply(lambda x: quarterly_churn_yes["Churn"] / total_churn).sum()
print(quarterly_churn_rate)Date
1    0.862159
2    0.085170
3    0.050803
4    0.051550
dtype: float64

The above code i have tried to the find churn rate grouped on date column querterly wise. I am getting 1,2,3,4 but i want year wise quarterly churn rate.

For example , if i have four years in the dataframe like 2018,2014,2017 in that

2008

1    1154
2     114
3      68
4      69

2014

1    1154
2     114
3      68
4      69
Answer

I think need:

out = pd.DataFrame({ 'Date': pd.to_datetime(['2015-01-01','2015-05-01','2015-07-01','2015-10-01','2015-04-01','2015-12-01','2016-01-01','2016-02-01','2015-05-01', '2015-10-01']), 'Churn': ['Yes'] * 8 + ['No'] * 2 })
print (out)Churn       Date
0   Yes 2015-01-01
1   Yes 2015-05-01
2   Yes 2015-07-01
3   Yes 2015-10-01
4   Yes 2015-04-01
5   Yes 2015-12-01
6   Yes 2016-01-01
7   Yes 2016-02-01
8    No 2015-05-01
9    No 2015-10-01

df = (out.loc[out['Churn'] == 'Yes'].groupby([out["Date"].dt.year,out["Date"].dt.quarter])["Churn"].count().rename_axis(('year','quarter')).reset_index(name='count'))print(df)year  quarter  count
0  2015        1      1
1  2015        2      2
2  2015        3      1
3  2015        4      2
4  2016        1      2

For separate DataFrames by years is possible create dictionary of DataFrames:

dfs = dict(tuple(out.groupby(out['Date'].dt.year)))
print (dfs)
{2016:   Churn       Date
6   Yes 2016-01-01
7   Yes 2016-02-01, 2015:   Churn       Date
0   Yes 2015-01-01
1   Yes 2015-05-01
2   Yes 2015-07-01
3   Yes 2015-10-01
4   Yes 2015-04-01
5   Yes 2015-12-01
8    No 2015-05-01
9    No 2015-10-01}print (dfs.keys())
dict_keys([2016, 2015])print (dfs[2015])Churn       Date
0   Yes 2015-01-01
1   Yes 2015-05-01
2   Yes 2015-07-01
3   Yes 2015-10-01
4   Yes 2015-04-01
5   Yes 2015-12-01
8    No 2015-05-01
9    No 2015-10-01Tenure column looks like thisout["tenure"].unique() 
Out[14]: 
array([ 8, 15, 32,  9, 48, 58, 10, 29,  1, 66, 24, 68,  4, 53,  6, 20, 52,49, 71,  2, 65, 67, 27, 18, 47, 45, 43, 59, 13, 17, 72, 61, 34, 11,35, 69, 63, 30, 19, 39,  3, 46, 54, 36, 12, 41, 50, 40, 28, 44, 51,33, 21, 70, 23, 16, 56, 14, 62,  7, 25, 31, 60,  5, 42, 22, 37, 64,57, 38, 26, 55])

It contains no of months, it seems like 1 to 72.

I need to split tenure column into "range".

For example, this column contains 1 to 72 numbers, need to range up to 4 range.

like 1 to 18 --> 1 range19 to 36 --> 2nd range37 to 54 --> 3rd range like that

here i found quarterlywise churn count and with that churn count later i found churn rate with churn count and total count.

quarterly_churn_yes = out.loc[out['Churn'] == 'Yes'].groupby([out["Date"].dt.year,out["Date"].dt.quarter]).count().rename_axis(('year','quarter'))
quarterly_churn_yes["Churn"]quarterly_churn_rate = out.groupby(out["Date"].dt.quarter).apply(lambda x: quarterly_churn_yes["Churn"] / total_churn).sum()
print(quarterly_churn_rate)

Like this I need to find tenure wise 4 range to find churn count.

https://en.xdnf.cn/q/119834.html

Related Q&A

splitting a list into two lists based on a unique value

I have a text file that looks something like this:hello 12 hello 56 world 25 world 26Is there a way in python that I can somehow parse the list that I obtain from reading this data to obtain two separa…

Customize axes in Matplotlib

I am a beginner with Python, Pandas and Matplotlib. I would like to customize the entries at the axes of a scatter plot. I have the following data:So on the x-axis there should be 5 entries, with the f…

how to show the max and min from user input?

Nevermindif xmin == 1:print(ymin)I tried using the max and min but I get a typeerror which is int object not iterable.

To output a string without whitespce

My program:def string_splosion(str):j=1c=len(str)i=0s=while(i<c):s=s+(str[:i+j])i=i+1print sprint("Enter a string:") s=raw_input() string_splosion(s)Sample input:Code Expected output:CCoCo…

Regex replace `a.b` to `a. b`? [duplicate]

This question already has answers here:Python: Replace with regex(2 answers)Closed 6 years ago.I am trying to change all strings of the type a.b to a. b. It should also work if any of the characters ar…

Is there something wrong with this line of Python code?

I tried this line of code, and it kept giving me the SyntaxError.print(/ / - / \ / | * 30, end=\r)^It pointed on the brackets. Any suggestions? Thanks!

TypeError: tuple indices must be integers or slices, not str postgres/python

Hi Im trying to get a timestamp from a row called time in my postgres database, however Im getting the error. Its the second row in the database. The error TypeError: tuple indices must be integers or …

Pandas Python: KeyError Date

I am import into python where it will automatically create a date time object.However I want the first column to be a datetime object in Python. Data looks likeDate,cost 41330.66667,100 41331.66667,101…

How to resample 1 minute data into 15 minute data?

CSV file. df before resample and after applying: df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" "))) df = df.set_index(pd.Datet…

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…