我正在尝试根据其他列中的条件创建一列。
一间房子有5个人年龄。我不需要按性别和年龄段计算那所房子中的个人人数。
我编写的代码不起作用
from pandas import DataFrame
df1 = pd.DataFrame({'member':[1,2], 'M1':[20,35],'M2':[27,42], 'M3':[77,62],'M4':[20,0],'M5':[0,35],
'G1':['M','F'],'G2':['M','F'],'G3':['M','F'],'G4':['M',0],'G5':[0,'F']})
#CODE WRITTEN
df1['M_20_to_30'] = ((df1[df1.columns[1:5]] >= 20) & (df1[df1.columns[1:5]] <= 30) & (df1[df1.columns[6:10]] == "M")).sum(1)
# EXPECTED OUTPUT
df1 = pd.DataFrame({'member':[1,2], 'M1':[20,35],'M2':[27,42], 'M3':[77,62],'M4':[20,0],'M5':[0,35],
'G1':['M','F'],'G2':['M','F'],'G3':['M','F'],'G4':['M',0],'G5':[0,'F'],'M_20_to_30':[2,0]})
答案 0 :(得分:0)
您可以这样做:
df1['M_20_to_30'] = (df1
.iloc[:,df1.columns.str.startswith('M')]
.apply(lambda x: sum(x.ge(20) & x.le(30))), 1))
member M1 M2 M3 M4 M5 G1 G2 G3 G4 G5 M_20_to_30
0 1 20 27 77 20 0 M M M M 0 3
1 2 35 42 62 0 35 F F F 0 F 0