我有一个这样的数据框:
case sex is1 is5 is10 im1 im5 im10
686 m 0.1406 0.2997 0.1490 0.1339 0.1350 0.1417
950 f 0.1602 0.3448 0.1719 0.0884 0.0962 0.1385
1005 f 0.1641 0.2606 0.1507 0.1306 0.1613 0.1328
1005 m 0.1456 0.3097 0.1825 0.1116 0.1295 0.1211
我想使用公式添加第9列,比如
if df['sex'] == 'm':
df['new col']=const1*df['is1']+const2*df['is5']+.....+const6*df['im10']
else:
df['new col'] = const7*df['is1']+.....const12*df['im10']
我该怎么做?
答案 0 :(得分:3)
试试这个:
df['new col'] = np.where(df.sex=='m',
df.drop(['case','sex'],1).dot(const_m),
df.drop(['case','sex'],1).dot(const_f))
其中const_m
是male
和const_f
的系数列表,是female
的系数列表