Pandas Series.str.upper() function
Series.str.upper() is a function in Pandas used to convert strings to uppercase.
During data processing, we often need to standardize the case of strings, such as converting abbreviations and titles to uppercase for easier reading and recognition.upper() The function converts all strings in a Series to uppercase.
Word meaning:upper means "upper or higher", here indicating converting letters to uppercase.
Basic syntax and parameters
str.upper() is a string accessor method of Series, so you need a Series containing strings first, and then call it through the .str accessor.
Syntax format
Series.str.upper()
Parameter Description
- Parameters: No parameters. This function requires no arguments and can be called directly.
Function description
- Return Value: Returns a Series containing uppercase strings; the new Series has the same length as the original.
- effect: Converts each string element in the Series to uppercase; non-string elements remain unchanged.
- Note: This function only converts alphabetic characters; numbers, punctuation, and other characters remain unchanged.
Example
Let us thoroughly master str.upper() through a series of examples from simple to complex.
Example 1: Basic Usage - Converting Simple Strings
Example
import
pandas
as
pd
Create a Series with lowercase strings
s
=
pd.
Series
(
[
'hello'
,
'world'
,
'runoob'
,
'python'
]
)
Use str.upper() to convert all strings to uppercase
result
=
s.
str
.
upper
(
)
(
"Original Series:"
)
(
s
)
(
"nConverted result:"
)
(
result
)
Output result:
原始 Series:
0 hello
1 world
2 runoob
3 python
dtype: object
Converted result:
0 HELLO
1 WORLD
2 RUNOOB
3 PYTHON
dtype: object
Code analysis:
pd.Series(['hello', 'world', 'runoob', 'python'])Created a Series containing lowercase strings.s.str.upper()Through the string accessor.strcalledupper()method, all strings are converted to uppercase.- The converted result is a new Series; the original Series remains unchanged.
Example 2: Converting Mixed-Case Strings
When a string contains mixed-case characters,upper() converts all lowercase letters to uppercase.
Example
import
pandas
as
pd
Create a Series with mixed-case strings
s
=
pd.
Series
(
[
'Hello World'
,
'runoob tutorial'
,
'PyThOn'
,
'Pandas'
]
)
Convert to uppercase with str.upper()
result
=
s.
str
.
upper
(
)
(
"Original Series:"
)
(
s
)
(
"nConverted result:"
)
(
result
)
Output result:
原始 Series:
0 Hello World
1 runoob tutorial
2 PyThOn
3 Pandas
dtype: object
Converted result:
0 HELLO WORLD
1 RUNOOB TUTORIAL
2 PYTHON
3 PANDAS
Code analysis:
- No matter how many lowercase letters a string contains,
upper()they are all converted to uppercase. - Spaces, numbers, and punctuation remain unchanged.
- This is very useful when standardizing data formats, such as converting country codes to uppercase.
Example 3: Handling Strings with Special Characters
upper() Only alphabetic characters are converted; other characters remain unchanged.
Example
import
pandas
as
pd
Create a Series with special characters
s
=
pd.
Series
(
[
'hello123'
,
'world@runoob'
,
'python!'
,
'a+b=c'
]
)
Convert to uppercase with str.upper()
result
=
s.
str
.
upper
(
)
(
"Original Series:"
)
(
s
)
(
"nConverted result:"
)
(
result
)
Output result:
原始 Series:
0 hello123
1 world@runoob
2 python!
3 a+b=c
dtype: object
Converted result:
0 HELLO123
1 WORLD@RUNOOB
2 PYTHON!
3 A+B=C
Code analysis:
- Digits (123) remain unchanged.
- Symbols (@, !, +, =) remain unchanged.
- Only alphabetic characters are converted to uppercase.
Example 4: Standardizing Country Code Format
In practical applications,upper() Often used to standardize formats such as country codes and currency codes.
Example
import
pandas
as
pd
User-entered country codes (possibly in any case)
country_codes
=
pd.
Series
(
[
'cn'
,
'us'
,
'Uk'
,
'Jp'
,
'fr'
]
)
Convert uniformly to uppercase
standardized
=
country_codes.
str
.
upper
(
)
(
"Original country codes:"
)
(
country_codes
)
(
"nAfter standardizing the format:"
)
(
standardized
)
Output result:
原始国家代码:
0 cn
1 us
2 Uk
3 Jp
4 fr
dtype: object
After unifying the format:
0 CN
1 US
2 UK
3 JP
4 FR
dtype: object
Notes
str.upper()Only alphabetic characters are converted; non-alphabetic characters such as numbers, spaces, and punctuation remain unchanged.- If the Series contains non-string elements (such as NaN, integers, etc.),
upper()they are left as-is and no error is raised. - This function returns a new Series and does not modify the original data.
- For Chinese strings, the characters are unaffected because Chinese has no concept of case.
Common Pandas functions