访问要在理解表达式中评估的列列表
有此dataframe称为frame
带有列:age
,成熟
,gend> gens> gend>,
,height> height
具有值'传递
'或'失败
'。
我想创建一个名为result
的新列,并计算具有check_columns
中的总体列的子集数量,该列中具有值传递
。
我尝试使用一种理解表达式,如这种情况所示,我希望将其评估为2,因为列age
成熟已经传递了
性别
具有失败
,这些在check_columns
的列中存在。
frame = pd.DataFrame(
data = {'Age':['PASSED'], 'Maturity':['PASSED'],'Gender':['FAILED'], 'Height':['PASSED']}
)
check_columns = ['Age','Maturity','Gender']
frame['result'] = sum([1 if frame[column] =='PASSED' else 0 for column in check_columns ])
我试图使用列表的理解力,但它说明了这个错误:
valueerror:系列的真实价值是模棱两可的。使用A.Empty,A.Bool(),A.Item(),a.any()或a.all()。
There is this dataframe called frame
with columns: Age
, Maturity
, Gender
, Height
which has values 'PASSED
' or 'FAILED
'.
I want to create a new column called result
and count the number of a subset of the overall columns as seen in check_columns
which has the value PASSED
.
I tried to use a comprehension expression which as seen in this case I wanted it to be evaluated to 2 since the columns Age
and Maturity
have PASSED
while Gender
has FAILED
which are present in a subset of columns in check_columns
.
frame = pd.DataFrame(
data = {'Age':['PASSED'], 'Maturity':['PASSED'],'Gender':['FAILED'], 'Height':['PASSED']}
)
check_columns = ['Age','Maturity','Gender']
frame['result'] = sum([1 if frame[column] =='PASSED' else 0 for column in check_columns ])
I tried to used a comprehension with a list but it says this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅选择所需的列,使用
dataframe.eq
检查哪些列等于“传递”,然后使用dataframe.sum.sum
(PassAXIS = AXIS =)来计数真实值1
计算列的计数)输出:
Select only the desired columns, check which ones are equal to 'PASSED' using
DataFrame.eq
, and count the True values usingDataFrame.sum
(passaxis=1
to count column-wise)Output: