Пример кода на Python:
import polars as pl
# Define a custom function that wraps the str.find() method
def find_substring(s, substring):
return int(s.find(substring))
# Test df
df = pl.DataFrame({
"text": ["testтестword",None,'']
})
# Apply the custom function to the "text" column using map_elements()
substr = 'word'
df = df.with_columns(
pl.col('text').str.find(substr,literal=True,strict=True).alias('in_polars'),
pl.col("text").map_elements(lambda s: find_substring(s, substr), return_dtype=pl.Int64).alias('find_check')
)
print(df)
Результаты:

В документации нет параметра для установки кодировки символов.
Использование моей функции — решение, но оно очень медленное.
Можете ли вы предложить что-нибудь побыстрее и без map_elements? Спасибо.
pl.col("text").map_elements(lambda s: find_substring(s, substr), return_dtype=pl.Int64).alias('find_check')
Подробнее здесь: https://stackoverflow.com/questions/791 ... en-using-u
Мобильная версия