为什么我得到的“str”对象没有属性“astype”`

2024-04-25 08:35:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到Student列包含Yes的索引。下面的代码片段是我的试用版,但没有运气和抛出'str' object has no attribute 'astype'错误

  rule=lambda x: x.astype(str).str.lower.contains('Yes')
  yesindex = teaching.loc[teaching['Students'].apply(rule)].index

有人能解释一下为什么会发生这种情况吗?我怎么可能解决呢


Tags: lambdano代码object错误attributerulestudent
1条回答
网友
1楼 · 发布于 2024-04-25 08:35:27

正如其他评论所提到的,可能您的学生列已经是字符串类型。如果您想得到它,请执行以下操作:

yesindex = deliveries.loc[deliveries['Students'].str.contains('Yes') == True].index

或者,如果要与小写字母匹配:

yesdindex = deliveries.loc[deliveries['Students'].str.lower().str.contains('yes') == True].index

相关问题 更多 >