当前位置:   article > 正文

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、结果输出

一、用法精讲

331、pandas.Series.str.repeat方法
331-1、语法
  1. # 331、pandas.Series.str.repeat方法
  2. pandas.Series.str.repeat(repeats)
  3. Duplicate each string in the Series or Index.
  4. Parameters:
  5. repeats
  6. int or sequence of int
  7. Same value for all (int) or different value per (sequence).
  8. Returns:
  9. Series or pandas.Index
  10. Series 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、代码示例
  1. # 331、pandas.Series.str.repeat方法
  2. import pandas as pd
  3. # 示例数据
  4. data = pd.Series(['abc', 'def', 'ghi'])
  5. # 使用'repeat'方法,所有字符串重复3次
  6. repeated_all = data.str.repeat(3)
  7. # 使用'repeat'方法,根据每个元素指定不同的重复次数
  8. repeats = pd.Series([1, 2, 3])
  9. repeated_individual = data.str.repeat(repeats)
  10. print("Original Series:\n", data)
  11. print("Series with all elements repeated 3 times:\n", repeated_all)
  12. print("Series with individual repeat counts:\n", repeated_individual)
331-6-3、结果输出
  1. # 331、pandas.Series.str.repeat方法
  2. # Original Series:
  3. # 0 abc
  4. # 1 def
  5. # 2 ghi
  6. # dtype: object
  7. # Series with all elements repeated 3 times:
  8. # 0 abcabcabc
  9. # 1 defdefdef
  10. # 2 ghighighi
  11. # dtype: object
  12. # Series with individual repeat counts:
  13. # 0 abc
  14. # 1 defdef
  15. # 2 ghighighi
  16. # dtype: object
332、pandas.Series.str.replace方法
332-1、语法
  1. # 332、pandas.Series.str.replace方法
  2. pandas.Series.str.replace(pat, repl, n=-1, case=None, flags=0, regex=False)
  3. Replace each occurrence of pattern/regex in the Series/Index.
  4. Equivalent to str.replace() or re.sub(), depending on the regex value.
  5. Parameters:
  6. pat
  7. str or compiled regex
  8. String can be a character sequence or regular expression.
  9. repl
  10. str or callable
  11. Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().
  12. n
  13. int, default -1 (all)
  14. Number of replacements to make from start.
  15. case
  16. bool, default None
  17. Determines if replace is case sensitive:
  18. If True, case sensitive (the default if pat is a string)
  19. Set to False for case insensitive
  20. Cannot be set if pat is a compiled regex.
  21. flags
  22. int, default 0 (no flags)
  23. Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.
  24. regex
  25. bool, default False
  26. Determines if the passed-in pattern is a regular expression:
  27. If True, assumes the passed-in pattern is a regular expression.
  28. If False, treats the pattern as a literal string
  29. Cannot be set to False if pat is a compiled regex or repl is a callable.
  30. Returns:
  31. Series or Index of object
  32. A copy of the object with all matching occurrences of pat replaced by repl.
  33. Raises:
  34. ValueError
  35. if regex is False and repl is a callable or pat is a compiled regex
  36. if pat is a compiled regex and case or flags is set
  37. Notes
  38. When 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、代码示例
  1. # 332、pandas.Series.str.replace方法
  2. import pandas as pd
  3. # 示例数据
  4. data = pd.Series(['abc_def', '123_456', 'ghi_jkl'])
  5. # 使用'replace'方法,简单字符串替换
  6. replaced_simple = data.str.replace('_', '-')
  7. # 使用'replace'方法,正则表达式替换(将数字替换为字母X)
  8. replaced_regex = data.str.replace(r'\d', 'X', regex=True)
  9. print("Original Series:\n", data)
  10. print("Series with simple replacement:\n", replaced_simple)
  11. print("Series with regex replacement:\n", replaced_regex)
332-6-3、结果输出
  1. # 332、pandas.Series.str.replace方法
  2. # Original Series:
  3. # 0 abc_def
  4. # 1 123_456
  5. # 2 ghi_jkl
  6. # dtype: object
  7. # Series with simple replacement:
  8. # 0 abc-def
  9. # 1 123-456
  10. # 2 ghi-jkl
  11. # dtype: object
  12. # Series with regex replacement:
  13. # 0 abc_def
  14. # 1 XXX_XXX
  15. # 2 ghi_jkl
  16. # dtype: object
333、pandas.Series.str.rfind方法
333-1、语法
  1. # 333、pandas.Series.str.rfind方法
  2. pandas.Series.str.rfind(sub, start=0, end=None)
  3. Return highest indexes in each strings in the Series/Index.
  4. 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().
  5. Parameters:
  6. sub
  7. str
  8. Substring being searched.
  9. start
  10. int
  11. Left edge index.
  12. end
  13. int
  14. Right edge index.
  15. Returns:
  16. 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、代码示例
  1. # 333、pandas.Series.str.rfind方法
  2. import pandas as pd
  3. # 示例数据
  4. data = pd.Series(['abcdef', 'abcabc', 'hello world'])
  5. # 使用'rfind'方法查找子字符串
  6. index_ef = data.str.rfind('ef')
  7. index_ab = data.str.rfind('ab')
  8. index_l = data.str.rfind('l')
  9. print("Original Series:\n", data)
  10. print("Position of 'ef':\n", index_ef)
  11. print("Position of 'ab':\n", index_ab)
  12. print("Position of 'l':\n", index_l)
333-6-3、结果输出
  1. # 333、pandas.Series.str.rfind方法
  2. # Original Series:
  3. # 0 abcdef
  4. # 1 abcabc
  5. # 2 hello world
  6. # dtype: object
  7. # Position of 'ef':
  8. # 0 4
  9. # 1 -1
  10. # 2 -1
  11. # dtype: int64
  12. # Position of 'ab':
  13. # 0 0
  14. # 1 3
  15. # 2 -1
  16. # dtype: int64
  17. # Position of 'l':
  18. # 0 -1
  19. # 1 -1
  20. # 2 9
  21. # dtype: int64
334、pandas.Series.str.rindex方法
334-1、语法
  1. # 334、pandas.Series.str.rindex方法
  2. pandas.Series.str.rindex(sub, start=0, end=None)
  3. Return highest indexes in each string in Series/Index.
  4. 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.
  5. Parameters:
  6. sub
  7. str
  8. Substring being searched.
  9. start
  10. int
  11. Left edge index.
  12. end
  13. int
  14. Right edge index.
  15. Returns:
  16. 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、代码示例
  1. # 334、pandas.Series.str.rindex方法
  2. import pandas as pd
  3. ser = pd.Series(["Deer", "eagle", "Sheep"])
  4. data = ser.str.rindex("e")
  5. print(data)
334-6-3、结果输出
  1. # 334、pandas.Series.str.rindex方法
  2. # 0 2
  3. # 1 4
  4. # 2 3
  5. # dtype: int64
335、pandas.Series.str.rjust方法
335-1、语法
  1. # 335、pandas.Series.str.rjust方法
  2. pandas.Series.str.rjust(width, fillchar=' ')
  3. Pad left side of strings in the Series/Index.
  4. Equivalent to str.rjust().
  5. Parameters:
  6. width
  7. int
  8. Minimum width of resulting string; additional characters will be filled with fillchar.
  9. fillchar
  10. str
  11. Additional character for filling, default is whitespace.
  12. Returns:
  13. 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、代码示例
  1. # 335、pandas.Series.str.rjust方法
  2. import pandas as pd
  3. # 示例数据
  4. data = pd.Series(['apple', 'banana', 'cherry'])
  5. # 将字符串右对齐,宽度为10,左侧填充'-'
  6. result = data.str.rjust(10, '-')
  7. print(result)
335-6-3、结果输出
  1. # 335、pandas.Series.str.rjust方法
  2. # 0 -----apple
  3. # 1 ----banana
  4. # 2 ----cherry
  5. # dtype: object
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/983582
推荐阅读
相关标签
  

闽ICP备14008679号