Pandas Series.str.strip()() 函数
Series.str.strip() 是 Pandas 中用于去除字符串两端空白字符的函数。
在数据处理过程中,我们经常需要清理用户输入或从文件读取的文本数据,这些数据两端可能包含多余的空格、制表符或换行符。strip() 函数可以去除字符串两端的空白字符,使数据更加干净整洁。
单词释义:strip 是"剥去、除去"的意思,在这里表示去除字符串两端的空白字符。
基本语法与参数
str.strip() 是 Series 的字符串访问器方法,因此你需要先有一个包含字符串的 Series,然后通过 .str 访问器来调用它。
语法格式
Series.str.strip(to_strip=None)
参数说明
| 参数 | 类型 | 是否必填 | 说明 | 默认值 |
|---|---|---|---|---|
| to_strip | str | 可选 | 指定要去除的字符集合。如果不指定,则默认去除空白字符(包括空格、制表符、换行符等)。 | None(空白字符) |
函数说明
- 返回值:返回一个去除两端空白字符后的新 Series。
- 效果:去除每个字符串元素两端的空白字符,中间的空白字符保持不变。
- 注意:只去除字符串两端的字符,中间的字符不会被影响。
实例
让我们通过一系列从简单到复杂的例子,彻底掌握 str.strip() 的用法。
示例 1:基础用法 - 去除两端空白
实例
import pandas as pd
# 创建包含空白字符的 Series
s = pd.Series([' hello ', 'tworldt', 'nrunoobn', ' python '])
# 使用 str.strip() 去除两端空白
result = s.str.strip()
print("原始 Series:")
print(repr(s)) # 使用 repr() 显示原始字符(包括不可见字符)
print("n去除空白后的结果:")
print(result)
# 创建包含空白字符的 Series
s = pd.Series([' hello ', 'tworldt', 'nrunoobn', ' python '])
# 使用 str.strip() 去除两端空白
result = s.str.strip()
print("原始 Series:")
print(repr(s)) # 使用 repr() 显示原始字符(包括不可见字符)
print("n去除空白后的结果:")
print(result)
输出结果:
原始 Series: 0 ' hello ' 1 'tworldt' 2 'nrunoobn' 3 ' python ' dtype: object 去除空白后的结果: 0 hello 1 world 2 runoob 3 python
代码解析:
pd.Series([' hello ', ...])创建了一个包含带空白字符的 Series。repr(s)用于显示字符串的原始内容,包括空格、制表符等不可见字符。s.str.strip()去除了每个字符串两端的空白字符。- 字符串中间的空白字符保持不变。
示例 2:去除指定字符
可以通过 to_strip 参数指定需要去除的字符。
实例
import pandas as pd
# 创建包含特定字符的 Series
s = pd.Series(['xxxhello xxx', 'yyyworldyyy', 'zzzrunoobzzz'])
# 去除两端的 'x' 字符
result_x = s.str.strip('x')
# 去除两端的 'y' 字符
result_y = s.str.strip('y')
# 去除两端的 'z' 字符
result_z = s.str.strip('z')
print("原始 Series:")
print(s)
print("n去除 'x' 后的结果:")
print(result_x)
print("n去除 'y' 后的结果:")
print(result_y)
print("n去除 'z' 后的结果:")
print(result_z)
# 创建包含特定字符的 Series
s = pd.Series(['xxxhello xxx', 'yyyworldyyy', 'zzzrunoobzzz'])
# 去除两端的 'x' 字符
result_x = s.str.strip('x')
# 去除两端的 'y' 字符
result_y = s.str.strip('y')
# 去除两端的 'z' 字符
result_z = s.str.strip('z')
print("原始 Series:")
print(s)
print("n去除 'x' 后的结果:")
print(result_x)
print("n去除 'y' 后的结果:")
print(result_y)
print("n去除 'z' 后的结果:")
print(result_z)
输出结果:
原始 Series: 0 xxxhello xxx 1 yyyworldyyy 2 zzzrunoobzzz dtype: object 去除 'x' 后的结果: 0 hello xxx 1 yyyworldyyy 2 zzzrunoobzzz 去除 'y' 后的结果: 0 xxxhello xxx 1 worldyyy 2 zzzrunoobzzz 去除 'z' 后的结果: 0 xxxhello xxx 1 yyyworldyyy 2 runoobzzz
代码解析:
strip('x')只会去除字符串两端的 'x' 字符。- 如果字符串中间有相同的字符,不会被去除。
- 只有连续出现在两端的字符才会被去除。
示例 3:处理真实数据中的空格问题
在处理从文件或用户输入的数据时,strip() 非常有用。
实例
import pandas as pd
# 模拟从表单收集的用户名数据(可能包含多余空格)
usernames = pd.Series([' admin ', 'user123', ' moderator ', 'guest'])
# 清理用户名
cleaned_usernames = usernames.str.strip()
print("原始用户名:")
print(usernames)
print("n清理后的用户名:")
print(cleaned_usernames)
# 去除空白后进行比较
print("n去除空白后是否相等:")
print(cleaned_usernames == 'admin')
# 模拟从表单收集的用户名数据(可能包含多余空格)
usernames = pd.Series([' admin ', 'user123', ' moderator ', 'guest'])
# 清理用户名
cleaned_usernames = usernames.str.strip()
print("原始用户名:")
print(usernames)
print("n清理后的用户名:")
print(cleaned_usernames)
# 去除空白后进行比较
print("n去除空白后是否相等:")
print(cleaned_usernames == 'admin')
输出结果:
原始用户名: 0 admin 1 user123 2 moderator 3 guest dtype: object 清理后的用户名: 0 admin 1 user123 2 moderator 3 guest 清理后的用户名: 0 True 1 False 2 False 3 False
代码解析:
strip()清理了用户名前后的空格。- 这确保了用户输入的
' admin '能正确匹配'admin'。 - 这是数据清洗的重要步骤,可以避免因空格导致的匹配失败。
相关函数
Pandas 还提供了其他去除空白的函数:
str.lstrip()- 只去除字符串左侧(开头)的空白字符。str.rstrip()- 只去除字符串右侧(结尾)的空白字符。
实例
import pandas as pd
s = pd.Series([' hello world '])
print("原始字符串:", repr(s[0]))
print("strip():", repr(s.str.strip()[0])) # 去除两端
print("lstrip():", repr(s.str.lstrip()[0])) # 去除左侧
print("rstrip():", repr(s.str.rstrip()[0])) # 去除右侧
s = pd.Series([' hello world '])
print("原始字符串:", repr(s[0]))
print("strip():", repr(s.str.strip()[0])) # 去除两端
print("lstrip():", repr(s.str.lstrip()[0])) # 去除左侧
print("rstrip():", repr(s.str.rstrip()[0])) # 去除右侧
输出结果:
原始字符串: ' hello world ' strip(): 'hello world' lstrip(): 'hello world ' rstrip(): ' hello world'
注意事项
str.strip()默认去除空白字符,包括空格、制表符(t)、换行符(n)、回车符(r)等。- 该函数只去除字符串两端的字符,中间的字符不会被影响。
- 如果 Series 中包含 NaN 值,
strip()会返回 NaN。 - 该函数返回一个新的 Series,不会修改原始数据。

Pandas 常用函数