Pandas Series.str.upper() Function

Pandas 常用函数Pandas Common Functions


Series.str.upper()It is a function in Pandas used to convert strings to uppercase.

In data processing, we often need to unify the case format of strings, such as converting abbreviations, titles, etc., to uppercase for easier reading and recognition.upper()The function can convert all strings in a Series to uppercase.

Word Meaning:upperIt means "above, higher", and here it indicates converting letters to uppercase.


Basic Syntax and Parameters

str.upper()is the string accessor method of Series, so you need to first have a Series containing strings, and then through the.straccessor to call it.

Syntax Format

Series.str.upper()

Parameter Description

  • Parameter: No parameters. This function does not require any parameters; just call it directly.

Function Description

  • Return Value: Returns a Series containing uppercase strings. The length of the new Series is the same as the original Series.
  • Effect: Converts each string element in the Series to uppercase; non-string elements remain unchanged.
  • Note: This function only converts alphabetic characters; digits, punctuation, and other characters remain unchanged.

Examples

Let's thoroughly master, through a series of examples from simple to complex,str.upper()the usage.

Example 1: Basic Usage - Converting Simple Strings

Example

import pandas as pd

# Create a Series containing lowercase strings
s = pd.Series(['hello', 'world', 'example', 'python'])

# Use str.upper() to convert all strings to uppercase
result = s.str.upper()

print("Original Series:")
print(s)
print("nConverted result:")
print(result)

Output result:

原始 Series:
0    hello
1    world
2    example
3    python
dtype: object

转换后的结果:
0    HELLO
1    WORLD
2    EXAMPLE
3    PYTHON
dtype: object

Code analysis:

  1. pd.Series(['hello', 'world', 'example', 'python'])Created a Series containing lowercase strings.
  2. s.str.upper()Through the string accessor.strcallupper()the method, converting all strings to uppercase.
  3. The converted result is a new Series; the original Series remains unchanged.

Example 2: Mixed-Case String Conversion

When a string contains mixed-case characters,upper()all lowercase letters will be converted to uppercase.

Example

import pandas as pd

# Create a Series containing mixed-case strings
s = pd.Series(['Hello World', 'example tutorial', 'PyThOn', 'Pandas'])

# Use str.upper() to convert to uppercase
result = s.str.upper()

print("Original Series:")
print(s)
print("nConverted result:")
print(result)

Output result:

原始 Series:
0    Hello World
1    example tutorial
2           PyThOn
3             Pandas
dtype: object

转换后的结果:
0    HELLO WORLD
1    EXAMPLE TUTORIAL
2           PYTHON
3             PANDAS

Code analysis:

  • No matter how many lowercase letters the string contains,upper()they will all be converted to uppercase.
  • Spaces, digits, and punctuation remain unchanged.
  • This is very useful when you need to unify data formats, such as converting country codes to uppercase.

Example 3: Handling Strings Containing Special Characters

upper()It only converts alphabetic characters; other characters remain unchanged.

Example

import pandas as pd

# Create a Series containing special characters
s = pd.Series(['hello123', 'world@example', 'python!', 'a+b=c'])

# Use str.upper() to convert to uppercase
result = s.str.upper()

print("Original Series:")
print(s)
print("nConverted result:")
print(result)

Output result:

原始 Series:
0         hello123
1    world@example
2           python!
3              a+b=c
dtype: object

转换后的结果:
0         HELLO123
1    WORLD@EXAMPLE
2           PYTHON!
3              A+B=C

Code analysis:

  • Digits (123) remain unchanged.
  • Symbols (@, !, +, =) remain unchanged.
  • Only alphabetic characters are converted to uppercase.

Example 4: Unifying Country Code Format

In practical applications,upper()it is often used to unify formats such as country codes and currency codes.

Example

import pandas as pd

# User-entered country code (could be any case)
country_codes = pd.Series(['cn', 'us', 'Uk', 'Jp', 'fr'])

# Convert to uppercase uniformly
standardized = country_codes.str.upper()

print("Original country codes:")
print(country_codes)
print("nAfter unifying format:")
print(standardized)

Output result:

原始国家代码:
0    cn
1    us
2    Uk
3    Jp
4    fr
dtype: object

统一格式后:
0    CN
1    US
2    UK
3    JP
4    FR
dtype: object

Notes

  • str.upper()It only converts alphabetic characters; non-alphabetic characters such as digits, spaces, and punctuation remain unchanged.
  • If the Series contains non-string elements (such as NaN, integers, etc.),upper()they remain as they are, and no error is thrown.
  • This function returns a new Series and does not modify the original data.
  • For Chinese strings, Chinese characters are not affected because Chinese does not have uppercase or lowercase.

Pandas 常用函数Pandas Common Functions

Other Extensions