polars.Series.str.head#

Series.str.head(n: int | IntoExprColumn) Series[source]#

返回字符串系列中每个字符串的前n个字符。

Parameters:
n

切片的长度(整数或表达式)。支持负索引;请参阅下面的注释(2)。

Returns:
Series

数据类型系列 String

注释

  1. n 输入是根据(UTF8)字符串中的字符数定义的。字符被定义为Unicode标量值。在处理ASCII文本时,单个字符由一个字节表示,否则最多由4个字节表示。

  2. n为负数时,head返回从字符串末尾到第n`th from the end of the string. For example, if `n = -3的所有字符,例如,如果`n = -3`,则返回除最后三个字符之外的所有字符。

  3. 如果字符串的长度少于n个字符,则返回完整的字符串。

示例

返回前5个字符。

>>> s = pl.Series(["pear", None, "papaya", "dragonfruit"])
>>> s.str.head(5)
shape: (4,)
Series: '' [str]
[
    "pear"
    null
    "papay"
    "drago"
]

返回从末尾开始的第3个字符。

>>> s = pl.Series(["pear", None, "papaya", "dragonfruit"])
>>> s.str.head(-3)
shape: (4,)
Series: '' [str]
[
    "p"
    null
    "pap"
    "dragonfr"
]