我知道我可以使用 str.contains() 来检查列中是否包含字符串,例如:
import polars as pl
df = pl.dataframe({"a": ["my name is bob","my little pony, my little pony"]})
(df.with_columns(bbb = pl.col('a').str.slice(1,10000).str.contains(pl.col('a').str.slice(0,10), literal=true)
)
)
我想要的是比赛的确切起始位置,而不仅仅是一个布尔值,如:
import re
x = re.search(r"pony","my little pony")
print(x.start(),x.end())
我怎样才能做到这一点?
正确答案
您可以使用series.str.find()
方法:
import polars as pl
df = pl.DataFrame({"a": ["my name is Bob","my little pony, my little pony"]})
df.with_columns(
bbb=pl.col('a').str.slice(1,10000).str.find(
pl.col('a').str.slice(0,10), literal=True)
)
)