Pandas Series.str.upper() 函数
Series.str.upper() 是 Pandas 中用于将字符串转换为大写的函数。
在数据处理过程中,我们经常需要统一字符串的大小写格式,比如将缩写词、标题等转换为大写形式,以便于阅读和识别。upper() 函数可以将 Series 中的所有字符串转换为大写形式。
单词释义:upper 是"上面的、较高的"意思,在这里表示将字母转换为大写。
基本语法与参数
str.upper() 是 Series 的字符串访问器方法,因此你需要先有一个包含字符串的 Series,然后通过 .str 访问器来调用它。
语法格式
Series.str.upper()
参数说明
- 参数:无参数。该函数不需要任何参数,直接调用即可。
函数说明
- 返回值:返回一个包含大写字符串的 Series,新 Series 的长度与原 Series 相同。
- 效果:将 Series 中的每个字符串元素转换为大写形式,非字符串元素保持不变。
- 注意:该函数只对字母字符进行转换,数字、标点符号等其他字符保持不变。
实例
让我们通过一系列从简单到复杂的例子,彻底掌握 str.upper() 的用法。
示例 1:基础用法 - 转换简单字符串
实例
import pandas as pd
# 创建一个包含小写字符串的 Series
s = pd.Series(['hello', 'world', 'runoob', 'python'])
# 使用 str.upper() 将所有字符串转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
# 创建一个包含小写字符串的 Series
s = pd.Series(['hello', 'world', 'runoob', 'python'])
# 使用 str.upper() 将所有字符串转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
输出结果:
原始 Series: 0 hello 1 world 2 runoob 3 python dtype: object 转换后的结果: 0 HELLO 1 WORLD 2 RUNOOB 3 PYTHON dtype: object
代码解析:
pd.Series(['hello', 'world', 'runoob', 'python'])创建了一个包含小写字符串的 Series。s.str.upper()通过字符串访问器.str调用upper()方法,将所有字符串转换为大写。- 转换后的结果是一个新的 Series,原 Series 保持不变。
示例 2:混合大小写字符串转换
当字符串中包含大小写混合的字符时,upper() 会将所有小写字母转换为大写。
实例
import pandas as pd
# 创建一个包含混合大小写字符串的 Series
s = pd.Series(['Hello World', 'runoob tutorial', 'PyThOn', 'Pandas'])
# 使用 str.upper() 转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
# 创建一个包含混合大小写字符串的 Series
s = pd.Series(['Hello World', 'runoob tutorial', 'PyThOn', 'Pandas'])
# 使用 str.upper() 转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
输出结果:
原始 Series: 0 Hello World 1 runoob tutorial 2 PyThOn 3 Pandas dtype: object 转换后的结果: 0 HELLO WORLD 1 RUNOOB TUTORIAL 2 PYTHON 3 PANDAS
代码解析:
- 无论字符串中包含多少个小写字母,
upper()都会将它们全部转换为大写。 - 空格、数字和标点符号保持不变。
- 这在需要统一数据格式时非常有用,比如将国家代码转换为大写。
示例 3:处理包含特殊字符的字符串
upper() 只会转换字母字符,其他字符保持不变。
实例
import pandas as pd
# 创建包含特殊字符的 Series
s = pd.Series(['hello123', 'world@runoob', 'python!', 'a+b=c'])
# 使用 str.upper() 转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
# 创建包含特殊字符的 Series
s = pd.Series(['hello123', 'world@runoob', 'python!', 'a+b=c'])
# 使用 str.upper() 转换为大写
result = s.str.upper()
print("原始 Series:")
print(s)
print("n转换后的结果:")
print(result)
输出结果:
原始 Series: 0 hello123 1 world@runoob 2 python! 3 a+b=c dtype: object 转换后的结果: 0 HELLO123 1 WORLD@RUNOOB 2 PYTHON! 3 A+B=C
代码解析:
- 数字(123)保持不变。
- 符号(@、!、+、=)保持不变。
- 只有字母字符被转换为大写。
示例 4:统一国家代码格式
在实际应用中,upper() 常用于统一国家代码、货币代码等格式。
实例
import pandas as pd
# 用户输入的国家代码(可能是任意大小写)
country_codes = pd.Series(['cn', 'us', 'Uk', 'Jp', 'fr'])
# 统一转换为大写
standardized = country_codes.str.upper()
print("原始国家代码:")
print(country_codes)
print("n统一格式后:")
print(standardized)
# 用户输入的国家代码(可能是任意大小写)
country_codes = pd.Series(['cn', 'us', 'Uk', 'Jp', 'fr'])
# 统一转换为大写
standardized = country_codes.str.upper()
print("原始国家代码:")
print(country_codes)
print("n统一格式后:")
print(standardized)
输出结果:
原始国家代码: 0 cn 1 us 2 Uk 3 Jp 4 fr dtype: object 统一格式后: 0 CN 1 US 2 UK 3 JP 4 FR dtype: object
注意事项
str.upper()只会转换字母字符,数字、空格、标点符号等非字母字符保持不变。- 如果 Series 中包含非字符串元素(如 NaN、整数等),
upper()会保持原样,不会抛出错误。 - 该函数返回一个新的 Series,不会修改原始数据。
- 对于中文字符串,中文字符不受影响,因为中文没有大小写之分。

Pandas 常用函数