为什么我会得到 `'str' 对象没有属性 'astype'`

1 投票
1 回答
3003 浏览
提问于 2025-06-18 04:10

我正在尝试找出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

有人能告诉我为什么会出现这个错误吗?我该怎么解决这个问题呢?

相关问题:

  • 暂无相关问题
暂无标签

1 个回答

1

正如其他评论提到的,你的“Students”这一列可能已经是字符串类型了。如果你想获取它,可以这样做:

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

或者如果你想要把它转换成小写来匹配的话,可以这样:

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

撰写回答