Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Series.str.upper() function

Pandas Series.str.upper() function

Pandas Common Functions Common Pandas functions


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

Function description


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

(

)

print

(

"Original Series:"

)

print

(

s

)

print

(

"nConverted result:"

)

print

(

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:

  1. pd.Series(['hello', 'world', 'runoob', 'python']) Created a Series containing lowercase strings.
  2. s.str.upper() Through the string accessor .str called upper() method, all strings are converted to uppercase.
  3. 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

(

)

print

(

"Original Series:"

)

print

(

s

)

print

(

"nConverted result:"

)

print

(

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:

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

(

)

print

(

"Original Series:"

)

print

(

s

)

print

(

"nConverted result:"

)

print

(

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:

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

(

)

print

(

"Original country codes:"

)

print

(

country_codes

)

print

(

"nAfter standardizing the format:"

)

print

(

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


Pandas Common Functions Common Pandas functions