Pandas Series.str.lower() Function

Pandas 常用函数Pandas Common Functions


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

In data processing, we often need to normalize the case format of strings, such as converting user-entered names, addresses, etc., to lowercase for unified comparison and searching.lower()The function can convert all strings in the Series to lowercase.

Word Meaning:lowerMeans "to lower, to make smaller"; here it indicates converting letters to lowercase.


Basic Syntax and Parameters

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

Syntax Format

Series.str.lower()

Parameter Description

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

Function Description

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

Examples

Let us thoroughly master, through a series of examples from simple to complex,str.lower()its usage.

Example 1: Basic Usage - Converting Simple Strings

Example

import pandas as pd

# Create a Series containing uppercase strings
s = pd.Series(['HELLO', 'WORLD', 'EXAMPLE', '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:

原始 Series:
0    HELLO
1    WORLD
2    EXAMPLE
3    PYTHON
dtype: object

转换后的结果:
0    hello
1    world
2    example
3    python
dtype: object

Code Explanation:

  1. pd.Series(['HELLO', 'WORLD', 'EXAMPLE', 'PYTHON'])Created a Series containing uppercase strings.
  2. s.str.lower()By using the string accessor.strto calllower()the 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 will convert all uppercase letters to lowercase.

Example

import pandas as pd

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

# Use str.lower() to convert to lowercase
result = s.str.lower()

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

Output:

原始 Series:
0    Hello World
1    EXAMPLE Tutorial
2           PyThOn
3             Pandas
dtype: object

转换后的结果:
0    hello world
1    example tutorial
2           python
3             pandas
dtype: object

Code Explanation:

  • No matter how many uppercase letters a string contains,lower()all of them will be converted to lowercase.
  • Spaces, digits, and punctuation remain unchanged.
  • This is very useful in data cleaning, for example, normalizing 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 Series of product names
products = pd.Series(['Apple', 'BANANA', 'Orange', 'Grape'])

# User-input search keyword (may be in any case)
search_keyword = 'BANANA'

# Convert both sides to lowercase for case-insensitive comparison
matches = products.str.lower() == search_keyword.lower()

print("Product list:")
print(products)
print(f"nSearch keyword: {search_keyword}")
print("Match result:")
print(products[matches])

Output:

产品列表:
0     Apple
1    BANANA
2    Orange
3     Grape
dtype: object

搜索关键词: BANANA
匹配结果:
1    BANANA
dtype: object

Code Explanation:

  • products.str.lower()Convert the product names to lowercase.
  • search_keyword.lower()Convert the search keyword to lowercase as well.
  • By comparing the two lowercase strings, case-insensitive matching is achieved.

Notes

  • str.lower()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.),lower()they will remain unchanged, and no error will be 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 case distinctions.

Pandas 常用函数Pandas Common Functions

Other Extensions