代码长,但是思路简单、直接,可以自定义一个函数
import pandas as pd import numpy as np sales = pd.read_csv("sales.csv") down = sales.quantile(0.25) mid = sales.quantile(0.5) up = sales.quantile(0.75) sales["R_Quartile"] = np.where( sales.recency <= down.recency, 4, np.where( sales.recency <= mid.recency, 3, np.where(sales.recency <= up.recency, 2, 1) ), ) sales["F_Quartile"] = np.where( sales.frequency <= down.frequency, 1, np.where( sales.frequency <= mid.frequency, 2, np.where(sales.frequency <= up.frequency, 3, 4), ), ) sales["M_Quartile"] = np.where( sales.monetary <= down.monetary, 1, np.where( sales.monetary <= mid.monetary, 2, np.where(sales.monetary <= up.monetary, 3, 4) ), ) print(sales.head(5))