我试图计算在一个spectific列“y”中具有数字2或3,4或5的人的百分比,但我得到以下错误:TypeError:|不支持的操作数类型:“str”和“bool”这是我的代码:All = df.shape[0]Seizure = df[df['y'] == 1]nonSeizure = (df[df['y'] == 2]) | (df[df['y'] == 3]) | (df[df['y'] == 4]) | (df[df['y'] == 5])x = len(Seizure)/Ally = len(nonSeizure)/Allprint('Seizure :',x*100,'%')print('non Seizure :',y*100,'%')
2 回答

肥皂起泡泡
TA贡献1829条经验 获得超6个赞
您可以使用熊猫数据帧的功能来实现所需的结果。isin
isin() 函数用于检查 DataFrame 中的每个元素是否包含在值中。
以下是您可能想要尝试的代码,
lst = [2, 3, 4, 5] # --> declare the list
All = df.shape[0]
Seizure = df[df['y'] == 1]
nonSeizure = df[df['y'].isin(lst)]
x = len(Seizure)/All
y = len(nonSeizure)/All
print('Seizure :',x*100,'%')
print('non Seizure :',y*100,'%')

蛊毒传说
TA贡献1895条经验 获得超3个赞
使用 ,因为它是一种更 python 的方式。您的代码可能会以这种方式编码。nonSeizure = df.loc[df['y'].isin([2, 3, 4, 5])]
添加回答
举报
0/150
提交
取消