为了账号安全,请及时绑定邮箱和手机立即绑定

如何在python中使用或运算符从csv中的列中选择多个值

如何在python中使用或运算符从csv中的列中选择多个值

慕斯王 2022-08-16 16:18:04
我试图计算在一个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,'%')


查看完整回答
反对 回复 2022-08-16
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

使用 ,因为它是一种更 python 的方式。您的代码可能会以这种方式编码。nonSeizure = df.loc[df['y'].isin([2, 3, 4, 5])]



查看完整回答
反对 回复 2022-08-16
  • 2 回答
  • 0 关注
  • 128 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号