Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.lower() Method



TheSeries.str.lower() method in in Python Pandas library is used to convert strings in a Series or Index to lowercase. This method is useful for text normalization and data preprocessing, as it ensures consistency in text data by converting all characters to lowercase.

Using this method can help in performing case-insensitive comparisons and analyses more effectively. And this is equivalent to Python's built-instr.lower() method and is commonly used in data cleaning and preprocessing tasks.

Syntax

Following is the syntax of the Pandas Series.str.lower() method −

Series.str.lower()

Parameters

The PandasSeries.str.lower() method does not accept any parameters.

Return Value

TheSeries.str.lower() method returns a Series or Index of the same shape, where each string has been converted to lowercase. This means that all characters in each string are converted to their lowercase form.

Example 1

Let's look at a basic example to understand how theSeries.str.lower() method works −

import pandas as pd# Create a Seriess = pd.Series(['Hello', 'WORLD', 'Pandas'])# Display the input Seriesprint("Input Series")print(s)# Apply the lower methodprint("Series after applying the lower:")print(s.str.lower())

When we run the above program, it produces the following result −

Input Series0    Hello1    WORLD2    Pandasdtype: objectSeries after applying the lower:0    hello1    world2    pandasdtype: object

Example 2

In this example, we'll demonstrate the use of theSeries.str.lower() method in a DataFrame −

import pandas as pd# Create a DataFramedf = pd.DataFrame({'Name': ['Alice', 'Bob', 'CHARLIE'], 'Role': ['ADMIN', 'User', 'Manager']})# Print the original DataFrameprint("Input DataFrame")print(df)# Apply the lower method to the 'Role' columndf['Role'] = df['Role'].str.lower()# Print the modified DataFrameprint("Modified DataFrame:")print(df)

Following is the output of the above code −

Input DataFrame    Name     Role0  Alice    ADMIN1    Bob     User2  CHARLIE  ManagerModified DataFrame    Name     Role0  Alice    admin1    Bob     user2  CHARLIE  manager

Example 3

Let's see another example where we applySeries.str.lower() method to an Index object of the pandas DataFrame.

import pandas as pd# Create a DataFrame with an Indexdf = pd.DataFrame({'Value': [1, 2, 3]}, index=['First', 'SECOND', 'THIRD'])# Print the original DataFrameprint("Original DataFrame:")print(df)# Apply lower to the DataFrame index labelsdf.index = df.index.str.lower()# Print the modified DataFrameprint("Modified DataFrame:")print(df)

Output of the above code is as follows −

Original DataFrame:        ValueFirst       1SECOND      2THIRD       3Modified DataFrame:        Valuefirst       1second      2third       3
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp