Pandas Series.str.lower() function
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
- Parameters: No parameters. This function requires no arguments and can be called directly.
Function description
- Return Value: Returns a Series containing lowercase strings; the new Series has the same length as the original.
- effect: Converts each string element in the Series to lowercase; 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.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
(
)
(
"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 uppercase strings.s.str.lower()Through the string accessor.strcalledlower()method, all strings are converted to lowercase.- 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
(
)
(
"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
dtype: object
Code analysis:
- No matter how many uppercase letters a string contains,
lower()they are all converted to lowercase. - Spaces, numbers, and punctuation remain unchanged.
- This is very useful in data cleaning, such as standardizing the format of user-entered names.
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
(
)
(
"Product list:"
)
(
products
)
(
f
"nSearch keyword: {search_keyword}"
)
(
"Matching result:"
)
(
products
[
matches
]
)
Output result:
产品列表:
0 Apple
1 BANANA
2 Orange
3 Grape
dtype: object
搜索关键词: BANANA
Match results:
1 BANANA
dtype: object
Code analysis:
products.str.lower()Convert product names to lowercase.search_keyword.lower()Convert the search keyword to lowercase as well.- By comparing two lowercase strings, case-insensitive matching is achieved.
Notes
str.lower()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.),
lower()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