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

Pandas Series.str.lower() function

Pandas Series.str.lower() function

Pandas Common Functions Common Pandas functions


Series.str.lower() is a function in Pandas used to convert strings to lowercase.

During data processing, we often need to standardize the case of strings, such as converting user-entered names and addresses to lowercase for consistent comparison and lookup.lower() The function converts all strings in a Series to lowercase.

Word meaning:lower means "to lower or make smaller"; here it means converting letters to lowercase.


Basic syntax and parameters

str.lower() 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.lower()

Parameter Description

Function description


Example

Let us thoroughly master str.lower() 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 uppercase strings

s

=

pd.

Series

(

[

'HELLO'

,

'WORLD'

,

'RUNOOB'

,

'PYTHON'

]

)

Use str.lower() to convert all strings to lowercase

result

=

s.

str

.

lower

(

)

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 uppercase strings.
  2. s.str.lower() Through the string accessor .str called lower() method, all strings are converted to lowercase.
  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,lower() it converts all uppercase letters to lowercase.

Example

import

pandas

as

pd

Create a Series with mixed-case strings

s

=

pd.

Series

(

[

'Hello World'

,

'RUNOOB Tutorial'

,

'PyThOn'

,

'Pandas'

]

)

Convert to lowercase with str.lower()

result

=

s.

str

.

lower

(

)

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

dtype: object

Code analysis:

Example 3: Case-Insensitive Comparison

lower() It is often used to implement case-insensitive string comparison.

Example

import

pandas

as

pd

Create a product name Series

products

=

pd.

Series

(

[

'Apple'

,

'BANANA'

,

'Orange'

,

'Grape'

]

)

User-entered search keyword (possibly in any case)

search_keyword

=

'BANANA'

Convert both sides to lowercase for a case-insensitive comparison

matches

=

products.

str

.

lower

(

)

==

search_keyword.

lower

(

)

print

(

"Product list:"

)

print

(

products

)

print

(

f

"nSearch keyword: {search_keyword}"

)

print

(

"Matching result:"

)

print

(

products

[

matches

]

)

Output result:

产品列表:

0     Apple

1    BANANA

2    Orange

3     Grape

dtype: object



搜索关键词: BANANA

Match results:

1    BANANA

dtype: object

Code analysis:


Notes


Pandas Common Functions Common Pandas functions