我正在阅读有关基于行为的新计算的博客,其中插入了新的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,则年龄较大
对于多种情况,您可以使用numpy.select
代替numpy.where
import numpy as np
cond = [df['age'] < 20, df['age'].between(20, 39), df['age'] >= 40]
choice = ['kid', 'young', 'old']
df['category'] = np.select(cond, choice)
# name age preTestScore postTestScore category
#0 Jason 42 4 25 old
#1 Molly 52 24 94 old
#2 Tina 36 31 57 young
#3 Jake 24 2 62 young
#4 Amy 73 3 70 old
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句