赞
踩
目录
331、pandas.Series.str.repeat方法
332、pandas.Series.str.replace方法
334、pandas.Series.str.rindex方法
- # 331、pandas.Series.str.repeat方法
- pandas.Series.str.repeat(repeats)
- Duplicate each string in the Series or Index.
-
- Parameters:
- repeats
- int or sequence of int
- Same value for all (int) or different value per (sequence).
-
- Returns:
- Series or pandas.Index
- Series or Index of repeated string objects specified by input parameter repeats.
331-2-1、repeat(必须):指定每个字符串要重复的次数,如果是单个整数值,则所有字符串都重复相同的次数;如果是pandas.Series或等长列表/数组,则每个字符串按照对应的位置值进行重复。
按照指定的次数重复每个字符串,这在生成大量重复数据或对某些特定字符串进行倍数扩展时非常有用。
返回一个新的Series,其中每个字符串都已经按照指定次数进行了重复,新的Series的索引与原Series保持一致。
无
无
- # 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、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方法
- 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:
- pat
- str or compiled regex
- String can be a character sequence or regular expression.
-
- repl
- str or callable
- 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().
-
- n
- int, default -1 (all)
- Number of replacements to make from start.
-
- case
- bool, default None
- Determines if replace is case sensitive:
-
- If True, case sensitive (the default if pat is a string)
-
- Set to False for case insensitive
-
- Cannot be set if pat is a compiled regex.
-
- flags
- int, default 0 (no flags)
- Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.
-
- regex
- bool, default False
- Determines 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 string
-
- Cannot be set to False if pat is a compiled regex or repl is a callable.
-
- Returns:
- Series or Index of object
- A copy of the object with all matching occurrences of pat replaced by repl.
-
- Raises:
- ValueError
- if regex is False and repl is a callable or pat is a compiled regex
-
- if pat is a compiled regex and case or flags is set
-
- Notes
-
- 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-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被视为普通字符串。
查找并替换字符串中的特定模式,它可以用于简单的字符串替换,也可以结合正则表达式实现复杂的模式替换。在数据清洗时,常用于纠正数据中的错误、去除不必要的字符或进行格式化。
返回一个新的Series,其中的字符串已经按照指定的模式进行了替换,新Series的索引与原Series保持一致。
无
无
- # 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、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方法
- 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:
- sub
- str
- Substring being searched.
-
- start
- int
- Left edge index.
-
- end
- int
- Right edge index.
-
- Returns:
- Series or Index of int.

333-2-1、sub(必须):字符串,表示要查找的子字符串。
333-2-2、start(可选,默认值为0):整数,指定从字符串的哪个位置开始查找,默认值为0,即从字符串的开头开始查找。
333-2-3、end(可选,默认值为None):整数,指定查找到字符串的哪个位置结束,默认值为None,表示查找到字符串的末尾。
查找指定子字符串在字符串中最后一次出现的位置,并返回其起始索引,如果子字符串未找到,则返回-1
,该方法对于需要定位字符串中某一特定部分的位置,并从后往前搜索的场景非常有用。
返回一个Series,其中每个元素对应原Series中字符串的子字符串最后一次出现的位置,如果子字符串不存在于某个元素中,则对应的返回值为-1
。
无
无
- # 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、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方法
- 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:
- sub
- str
- Substring being searched.
-
- start
- int
- Left edge index.
-
- end
- int
- Right edge index.
-
- Returns:
- Series or Index of object.

334-2-1、sub(必须):字符串,表示要查找的子字符串。
334-2-2、start(可选,默认值为0):整数,指定从字符串的哪个位置开始查找,默认值为0,即从字符串的开头开始查找。
334-2-3、end(可选,默认值为None):整数,指定查找到字符串的哪个位置结束,默认值为None,表示查找到字符串的末尾。
查找指定子字符串在字符串中最后一次出现的位置,并返回其起始索引,与rfind()不同的是,如果子字符串未找到,rindex()会抛出ValueError而不是返回-1
。
返回一个Series,其中每个元素对应原Series中字符串的子字符串最后一次出现的位置,如果子字符串不存在,则抛出ValueError。
无
无
- # 334、pandas.Series.str.rindex方法
- import pandas as pd
- ser = pd.Series(["Deer", "eagle", "Sheep"])
- data = ser.str.rindex("e")
- print(data)
- # 334、pandas.Series.str.rindex方法
- # 0 2
- # 1 4
- # 2 3
- # dtype: int64
- # 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:
- width
- int
- Minimum width of resulting string; additional characters will be filled with fillchar.
-
- fillchar
- str
- Additional character for filling, default is whitespace.
-
- Returns:
- Series/Index of objects.

335-2-1、width(必须):整数,指定字符串对齐后的总宽度,如果字符串的长度小于width,那么字符串会被填充字符以达到这个宽度;如果字符串的长度已经大于或等于width,则原字符串保持不变,不会进行截断或修改。
335-2-2、fillchar(可选,默认值为' '):字符串,表示用于填充字符串左侧的字符,该参数只能接受一个字符,如果提供的fillchar是多个字符,会抛出TypeError。
用于将字符串向右对齐并使其达到指定的宽度,如果字符串本身的长度小于width,则在左侧填充指定的字符fillchar,使字符串的总长度达到width;如果字符串的长度已经大于或等于width,则返回原字符串。
返回一个与原Series长度相同的新的Series对象,且其中的每个元素都是经过右对齐处理后的字符串。
无
无
- # 335、pandas.Series.str.rjust方法
- import pandas as pd
- # 示例数据
- data = pd.Series(['apple', 'banana', 'cherry'])
- # 将字符串右对齐,宽度为10,左侧填充'-'
- result = data.str.rjust(10, '-')
- print(result)
- # 335、pandas.Series.str.rjust方法
- # 0 -----apple
- # 1 ----banana
- # 2 ----cherry
- # dtype: object
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。