如何根据条件更新数据框中的列

数据挖掘 Python 数据框
2022-03-06 21:35:27

如何根据 Lvalue 列更新数据框中的 IsLCap 列,无论它是否被大写。

df.loc[df.Lvalue.istitle(), 'IsLCap'] = 1 # 需要修正

收到错误-AttributeError:“系列”对象没有属性“istitle”

1个回答

您可以使用 访问字符串函数.str这应该有效:

df.loc[df.Lvalue.str.istitle(), 'IsLCap'] = 1

更新 要检查列中字符串的长度,您可以使用 string 方法.str.len

df.loc[df.Lvalue.str.len() < 3, 'less_than_three'] = 1

Pandas 系列字符串方法