如何基于包含整数或字符串的其他列创建列?

时间:2018-10-26 20:22:23

标签: python python-3.x pandas numpy

我需要按月销售时的年龄将数据集中的产品分类。我将分类基于表示数据集中月份的数字索引值。

但是,在此分类中,我还需要考虑“产品类型”和“业务类型”等列下的字符串值。我如何最有效地做到这一点?

到目前为止,仅考虑数字值,这就是我的代码的样子:

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')

0 个答案:

没有答案