我需要按月销售时的年龄将数据集中的产品分类。我将分类基于表示数据集中月份的数字索引值。
但是,在此分类中,我还需要考虑“产品类型”和“业务类型”等列下的字符串值。我如何最有效地做到这一点?
到目前为止,仅考虑数字值,这就是我的代码的样子:
ProductAgeFunctions = [df['LaunchMonthIndex'] == 0,
(df['MonthIndex'] - df['LaunchMonthIndex'] <= 23),
(df['MonthIndex'] - df['LaunchMonthIndex'] >= 24) & (df['MonthYearIndex']
- df['LaunchMonthIndex'] <= 47),
(df['MonthYearIndex'] - df['LaunchMonthIndex'] >= 48)]
ProductAgeChoices = ['N/A', 'Up to 2 Years', '2 to 4 Years', 'More Than 4 Years']
df['ProductAgeCategories'] = np.select(ProductAgeFunctions, ProductAgeChoices, default='Other')