def rfm_segment(row): if row['R_Score'] >= 4 and row['F_Score'] >= 4 and row['M_Score'] >= 4: return 'Champions' elif row['F_Score'] >= 4: return 'Loyal Customers' elif row['M_Score'] >= 4: return 'Big Spenders' elif row['R_Score'] <= 2: return 'At-Risk' else: return 'Others' rfm['Segment'] = rfm.apply(rfm_segment, axis=1)