> 技术文档 > Python酷库之旅-第三方库Pandas(080)

Python酷库之旅-第三方库Pandas(080)

目录

一、用法精讲

331、pandas.Series.str.repeat方法

331-1、语法

331-2、参数

331-3、功能

331-4、返回值

331-5、说明

331-6、用法

331-6-1、数据准备

331-6-2、代码示例

331-6-3、结果输出

332、pandas.Series.str.replace方法

332-1、语法

332-2、参数

332-3、功能

332-4、返回值

332-5、说明

332-6、用法

332-6-1、数据准备

332-6-2、代码示例

332-6-3、结果输出

333、pandas.Series.str.rfind方法

333-1、语法

333-2、参数

333-3、功能

333-4、返回值

333-5、说明

333-6、用法

333-6-1、数据准备

333-6-2、代码示例

333-6-3、结果输出

334、pandas.Series.str.rindex方法

334-1、语法

334-2、参数

334-3、功能

334-4、返回值

334-5、说明

334-6、用法

334-6-1、数据准备

334-6-2、代码示例

334-6-3、结果输出

335、pandas.Series.str.rjust方法

335-1、语法

335-2、参数

335-3、功能

335-4、返回值

335-5、说明

335-6、用法

335-6-1、数据准备

335-6-2、代码示例

335-6-3、结果输出

 二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

331、pandas.Series.str.repeat方法
331-1、语法
# 331、pandas.Series.str.repeat方法pandas.Series.str.repeat(repeats)Duplicate each string in the Series or Index.Parameters:repeatsint or sequence of intSame value for all (int) or different value per (sequence).Returns:Series or pandas.IndexSeries or Index of repeated string objects specified by input parameter repeats.
331-2、参数

331-2-1、repeat(必须)指定每个字符串要重复的次数,如果是单个整数值,则所有字符串都重复相同的次数;如果是pandas.Series或等长列表/数组,则每个字符串按照对应的位置值进行重复。

331-3、功能

        按照指定的次数重复每个字符串,这在生成大量重复数据或对某些特定字符串进行倍数扩展时非常有用。

331-4、返回值

        返回一个新的Series,其中每个字符串都已经按照指定次数进行了重复,新的Series的索引与原Series保持一致。

331-5、说明

        无

331-6、用法
331-6-1、数据准备
331-6-2、代码示例
# 331、pandas.Series.str.repeat方法import pandas as pd# 示例数据data = pd.Series([\'abc\', \'def\', \'ghi\'])# 使用\'repeat\'方法,所有字符串重复3次repeated_all = data.str.repeat(3)# 使用\'repeat\'方法,根据每个元素指定不同的重复次数repeats = pd.Series([1, 2, 3])repeated_individual = data.str.repeat(repeats)print(\"Original Series:\\n\", data)print(\"Series with all elements repeated 3 times:\\n\", repeated_all)print(\"Series with individual repeat counts:\\n\", repeated_individual)
331-6-3、结果输出
# 331、pandas.Series.str.repeat方法# Original Series:# 0 abc# 1 def# 2 ghi# dtype: object# Series with all elements repeated 3 times:# 0 abcabcabc# 1 defdefdef# 2 ghighighi# dtype: object# Series with individual repeat counts:# 0 abc# 1 defdef# 2 ghighighi# dtype: object
332、pandas.Series.str.replace方法
332-1、语法
# 332、pandas.Series.str.replace方法pandas.Series.str.replace(pat, repl, n=-1, case=None, flags=0, regex=False)Replace each occurrence of pattern/regex in the Series/Index.Equivalent to str.replace() or re.sub(), depending on the regex value.Parameters:patstr or compiled regexString can be a character sequence or regular expression.replstr or callableReplacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().nint, default -1 (all)Number of replacements to make from start.casebool, default NoneDetermines if replace is case sensitive:If True, case sensitive (the default if pat is a string)Set to False for case insensitiveCannot be set if pat is a compiled regex.flagsint, default 0 (no flags)Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.regexbool, default FalseDetermines if the passed-in pattern is a regular expression:If True, assumes the passed-in pattern is a regular expression.If False, treats the pattern as a literal stringCannot be set to False if pat is a compiled regex or repl is a callable.Returns:Series or Index of objectA copy of the object with all matching occurrences of pat replaced by repl.Raises:ValueErrorif regex is False and repl is a callable or pat is a compiled regexif pat is a compiled regex and case or flags is setNotesWhen pat is a compiled regex, all flags should be included in the compiled regex. Use of case, flags, or regex=False with a compiled regex will raise an error.
332-2、参数

332-2-1、pat(必须)字符串或正则表达式,表示要替换的模式。如果regex=True,则pat是正则表达式;否则pat被视为普通字符串。

332-2-2、repl(必须)用于替换pat的内容,如果是字符串,则直接替换为该字符串;如果是一个函数(callable),则它会被每个匹配的元素调用,返回值作为替换的内容。

332-2-3、n(可选,默认值为-1)整数,指定要替换的最大次数,默认为-1,表示替换所有匹配的内容;如果设定为一个正整数,则只替换指定次数。

332-2-4、case(可选,默认值为None)布尔值,如果为True,替换过程区分大小写;如果为False,则不区分大小写;默认为None,这时的行为取决于是否使用正则表达式。

332-2-5、flags(可选,默认值为0)整数,正则表达式的标志(flags),可以用来控制匹配行为,如re.IGNORECASE等。

332-2-6、regex(可选,默认值为False)布尔值,指定pat是否被解释为正则表达式,如果为False,则pat被视为普通字符串。

332-3、功能

        查找并替换字符串中的特定模式,它可以用于简单的字符串替换,也可以结合正则表达式实现复杂的模式替换。在数据清洗时,常用于纠正数据中的错误、去除不必要的字符或进行格式化。

332-4、返回值

        返回一个新的Series,其中的字符串已经按照指定的模式进行了替换,新Series的索引与原Series保持一致。

332-5、说明

        无

332-6、用法
332-6-1、数据准备
332-6-2、代码示例
# 332、pandas.Series.str.replace方法import pandas as pd# 示例数据data = pd.Series([\'abc_def\', \'123_456\', \'ghi_jkl\'])# 使用\'replace\'方法,简单字符串替换replaced_simple = data.str.replace(\'_\', \'-\')# 使用\'replace\'方法,正则表达式替换(将数字替换为字母X)replaced_regex = data.str.replace(r\'\\d\', \'X\', regex=True)print(\"Original Series:\\n\", data)print(\"Series with simple replacement:\\n\", replaced_simple)print(\"Series with regex replacement:\\n\", replaced_regex)
332-6-3、结果输出
# 332、pandas.Series.str.replace方法# Original Series:# 0 abc_def# 1 123_456# 2 ghi_jkl# dtype: object# Series with simple replacement:# 0 abc-def# 1 123-456# 2 ghi-jkl# dtype: object# Series with regex replacement:# 0 abc_def# 1 XXX_XXX# 2 ghi_jkl# dtype: object
333、pandas.Series.str.rfind方法
333-1、语法
# 333、pandas.Series.str.rfind方法pandas.Series.str.rfind(sub, start=0, end=None)Return highest indexes in each strings in the Series/Index.Each of returned indexes corresponds to the position where the substring is fully contained between [start:end]. Return -1 on failure. Equivalent to standard str.rfind().Parameters:substrSubstring being searched.startintLeft edge index.endintRight edge index.Returns:Series or Index of int.
333-2、参数

333-2-1、sub(必须)字符串,表示要查找的子字符串。

333-2-2、start(可选,默认值为0)整数,指定从字符串的哪个位置开始查找,默认值为0,即从字符串的开头开始查找。

333-2-3、end(可选,默认值为None)整数,指定查找到字符串的哪个位置结束,默认值为None,表示查找到字符串的末尾。

333-3、功能

        查找指定子字符串在字符串中最后一次出现的位置,并返回其起始索引,如果子字符串未找到,则返回-1,该方法对于需要定位字符串中某一特定部分的位置,并从后往前搜索的场景非常有用。

333-4、返回值

        返回一个Series,其中每个元素对应原Series中字符串的子字符串最后一次出现的位置,如果子字符串不存在于某个元素中,则对应的返回值为-1

333-5、说明

        无

333-6、用法
333-6-1、数据准备
333-6-2、代码示例
# 333、pandas.Series.str.rfind方法import pandas as pd# 示例数据data = pd.Series([\'abcdef\', \'abcabc\', \'hello world\'])# 使用\'rfind\'方法查找子字符串index_ef = data.str.rfind(\'ef\')index_ab = data.str.rfind(\'ab\')index_l = data.str.rfind(\'l\')print(\"Original Series:\\n\", data)print(\"Position of \'ef\':\\n\", index_ef)print(\"Position of \'ab\':\\n\", index_ab)print(\"Position of \'l\':\\n\", index_l)
333-6-3、结果输出
# 333、pandas.Series.str.rfind方法# Original Series:# 0 abcdef# 1 abcabc# 2 hello world# dtype: object# Position of \'ef\':# 0 4# 1 -1# 2 -1# dtype: int64# Position of \'ab\':# 0 0# 1 3# 2 -1# dtype: int64# Position of \'l\':# 0 -1# 1 -1# 2 9# dtype: int64
334、pandas.Series.str.rindex方法
334-1、语法
# 334、pandas.Series.str.rindex方法pandas.Series.str.rindex(sub, start=0, end=None)Return highest indexes in each string in Series/Index.Each of the returned indexes corresponds to the position where the substring is fully contained between [start:end]. This is the same as str.rfind except instead of returning -1, it raises a ValueError when the substring is not found. Equivalent to standard str.rindex.Parameters:substrSubstring being searched.startintLeft edge index.endintRight edge index.Returns:Series or Index of object.
334-2、参数

334-2-1、sub(必须)字符串,表示要查找的子字符串。

334-2-2、start(可选,默认值为0)整数,指定从字符串的哪个位置开始查找,默认值为0,即从字符串的开头开始查找。

334-2-3、end(可选,默认值为None)整数,指定查找到字符串的哪个位置结束,默认值为None,表示查找到字符串的末尾。

334-3、功能

        查找指定子字符串在字符串中最后一次出现的位置,并返回其起始索引,与rfind()不同的是,如果子字符串未找到,rindex()会抛出ValueError而不是返回-1

334-4、返回值

        返回一个Series,其中每个元素对应原Series中字符串的子字符串最后一次出现的位置,如果子字符串不存在,则抛出ValueError。

334-5、说明

        无

334-6、用法
334-6-1、数据准备
334-6-2、代码示例
# 334、pandas.Series.str.rindex方法import pandas as pdser = pd.Series([\"Deer\", \"eagle\", \"Sheep\"])data = ser.str.rindex(\"e\")print(data)
334-6-3、结果输出
# 334、pandas.Series.str.rindex方法# 0 2# 1 4# 2 3# dtype: int64
335、pandas.Series.str.rjust方法
335-1、语法
# 335、pandas.Series.str.rjust方法pandas.Series.str.rjust(width, fillchar=\' \')Pad left side of strings in the Series/Index.Equivalent to str.rjust().Parameters:widthintMinimum width of resulting string; additional characters will be filled with fillchar.fillcharstrAdditional character for filling, default is whitespace.Returns:Series/Index of objects.
335-2、参数

335-2-1、width(必须)整数,指定字符串对齐后的总宽度,如果字符串的长度小于width,那么字符串会被填充字符以达到这个宽度;如果字符串的长度已经大于或等于width,则原字符串保持不变,不会进行截断或修改。

335-2-2、fillchar(可选,默认值为\' \')字符串,表示用于填充字符串左侧的字符,该参数只能接受一个字符,如果提供的fillchar是多个字符,会抛出TypeError。

335-3、功能

        用于将字符串向右对齐并使其达到指定的宽度,如果字符串本身的长度小于width,则在左侧填充指定的字符fillchar,使字符串的总长度达到width;如果字符串的长度已经大于或等于width,则返回原字符串。

335-4、返回值

        返回一个与原Series长度相同的新的Series对象,且其中的每个元素都是经过右对齐处理后的字符串。

335-5、说明

        无

335-6、用法
335-6-1、数据准备
335-6-2、代码示例
# 335、pandas.Series.str.rjust方法import pandas as pd# 示例数据data = pd.Series([\'apple\', \'banana\', \'cherry\'])# 将字符串右对齐,宽度为10,左侧填充\'-\'result = data.str.rjust(10, \'-\')print(result)
335-6-3、结果输出
# 335、pandas.Series.str.rjust方法# 0 -----apple# 1 ----banana# 2 ----cherry# dtype: object

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页