【发布时间】:2018-05-04 02:37:14
【问题描述】:
我正在阅读一篇关于基于条件的新计算的博客,其中插入了新的 col 'category'。
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, 2, 3],
'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(data, columns = ['name', 'age', 'preTestScore', 'postTestScore'])
df['category'] = np.where(df['age']>=50, 'yes', 'no')
如何将其扩展到更多的条件,例如 如果年龄小于 20 则为孩子; 如果在 21 到 40 之间,那么年轻; 40岁以上就老了
【问题讨论】: