RFM Analysis of a Healthcare Dataset Using Python
Published: 2023-10-30 20:12:47

The dataset contains data of 4 different diseases and the cost patients from 10 different states incurred during treatment in July 2023. The first step is installing the necessary Python libraries and the dataset:

```
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go
pio.templates.default = "plotly_white"

data = pd.read_excel("/kaggle/input/hiv-new-cases-2017-ke/HIV New cases 2017.xlsx")
print(data.head())
```

  PatientID TreatmentDate  AmountSpent in $    DiseaseType  TreatmentNumber  \
0       4600    2023-07-17             28226  Heart disease           562915   
1       6302    2023-07-17               979   Hypertension           563003   
2       2456    2023-07-17               947   Hypertension           878136   
3       5602    2023-07-17               703       Diabetes           120090   
4       1811    2023-07-17             27380    Lung Cancer           140526   

        State  
0  California  
1       Texas  
2    Kentucky  
3    Delaware  
4      Alaska  
Calculating RFM Values
The next step is to calculate the Recency, Frequency, and Monetary values of the patients.
from datetime import datetime


```
# Convert 'TreatmentDate' to datetime
data['TreatmentDate'] = pd.to_datetime(data['TreatmentDate'])

# Calculate Recency
data['recency'] = pd.to_datetime('2023-10-17') - data['TreatmentDate']

# Calculate Frequency
data['frequency'] = data.groupby('PatientID').agg({'TreatmentNumber': 'nunique'})
data['frequency'] = data_frequency.rename(columns={'TreatmentNumber': 'frequency'})

# Calculate Monetary Value
data['monetary'] = data.groupby('PatientID').agg({'AmountSpent in $': 'sum'})
data['monetary'] = data_monetary.rename(columns={'AmountSpent in $': 'monetary_value'})
print(data.head())
```

  PatientID TreatmentDate  AmountSpent in $    DiseaseType  TreatmentNumber  \
0       4600    2023-07-17             28226  Heart disease           562915   
1       6302    2023-07-17               979   Hypertension           563003   
2       2456    2023-07-17               947   Hypertension           878136   
3       5602    2023-07-17               703       Diabetes           120090   
4       1811    2023-07-17             27380    Lung Cancer           140526   

        State recency  frequency  monetary  
0  California 92 days        1       28226 
1       Texas 92 days        1       979
2    Kentucky 92 days        1       947  
3    Delaware 92 days        1      703  
4      Alaska 92 days        1      27380 
________________________________________

Calculating RFM Scores
This step involves calculating the recency, frequency, and monetary scores

```
# Define score ranges and assign scores to recency, frequency, and monetary value
recency_scores = pd.qcut(df['recency'].dt.days, q=5, labels=False)
frequency_scores = pd.qcut(df_frequency['frequency'], q=5, labels=False)
monetary_scores = pd.qcut(df_monetary['monetary_value'], q=5, labels=False)

# Assign the calculated scores to the DataFrame
df['recency_score'] = recency_scores
df_frequency['frequency_score'] = frequency_scores
df_monetary['monetary_score'] = monetary_scores
```


  PatientID TreatmentDate  AmountSpent in $    DiseaseType  TreatmentNumber  \
0       4600    2023-07-17             28226  Heart disease           562915   
1       6302    2023-07-17               979   Hypertension           563003   
2       2456    2023-07-17               947   Hypertension           878136   
3       5602    2023-07-17               703       Diabetes           120090   
4       1811    2023-07-17             27380    Lung Cancer           140526   

        State recency  frequency  monetary  RecencyScore  FrequencyScore  \
0  California 92 days        1       28226 		1		1
1       Texas 92 days        1       979		1		1
2    Kentucky 92 days        1       947  		1		1
3    Delaware 92 days        1      703  		1		1
4      Alaska 92 days        1      27380 		1		1
MonetaryScore  RFM_Score Value Segment  
0              2          4     	Low-Value  
1              1          3     	Low-Value  
2              1          3     	Low-Value  
3              1          3    	Low-Value  
4              2          4     	Low-Value  

Combining RFM Scores

```
# Combine the RFM scores into a single RFM score
df['RFM_score'] = df['recency_score'].astype(str) + df_frequency['frequency_score'].astype(str) + df_monetary['monetary_score'].astype(str)

# print data
```
  







  PatientID TreatmentDate  AmountSpent in $    TreatmentNumber  
0       4600    2023-07-17             28226  	562915   
1       6302    2023-07-17               979   	563003   
2       2456    2023-07-17               947  		878136   
3       5602    2023-07-17               703       	120090   
4       1811    2023-07-17             27380    	140526   

        recency  frequency  monetary  RecencyScore  FrequencyScore  
0      92 days        1       28226 		1		1
1       92 days        1       979		1		1
2    92 days        1       947  		1		1
3    92 days        1      703  		1		1
4      92 days        1      27380 		1		1
MonetaryScore  RFM_Score 
0              2          4     	
1              1          3     	 
2              1          3     	  
3              1          3    	  
4              2          4     	 

Lastly, the RFM_score column combines the individual scores for recency, frequency, and monetary value into a single RFM score. This score can be used to segment customers and gain insights into their behavior and preferences.