
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Pandas Series.str.casefold() Method
TheSeries.str.casefold() method in Pandas is used to convert strings in a Series or Index to be casefolded. Casefolding is a more aggressive form of lower casing used for text normalization. It is especially useful for performing case-insensitive comparisons and for handling text in a more uniform manner.
This method is equivalent to Python's built-instr.casefold() method and is typically used to standardize text data in data analysis tasks.
Syntax
Following is the syntax of the Pandas Series.str.casefold() method −
Series.str.casefold()
Parameters
The PandasSeries.str.casefold() method does not accept any parameters.
Return Value
TheSeries.str.casefold() method returns a Series or Index of the same shape, where each string has been casefolded. This means that all characters in each string are converted to their casefolded (lowercase) form.
Example 1
Let's look at a basic example to understand how theSeries.str.casefold() method works −
import pandas as pd# Create a Seriess = pd.Series(['Hi', 'WELCOME to', 'TUTORIALSPOINT'])# Display the input Seriesprint("Input Series")print(s)# Apply the casefold methodprint("Series after applying the casefold:")print(s.str.casefold())When we run the above program, it produces the following result −
Input Series0 Hi1 WELCOME to2 TUTORIALSPOINTdtype: objectSeries after applying the casefold:0 hi1 welcome to2 tutorialspointdtype: object
Example 2
In this example, we'll demonstrate the use of theSeries.str.casefold() method in a DataFrame −
import pandas as pd# Create a DataFramedf = pd.DataFrame({'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Subject': ['Math', 'English', 'Science', 'Music', 'Games']})# Print the original DataFrameprint("Input DataFrame")print(df)# Apply the casefold method to the 'Day' columndf.Day = df.Day.str.casefold()# Print the modified DataFrameprint("Modified DataFrame:")print(df)Following is the output of the above code −
Original DataFrame: Day Subject0 Mon Math1 Tue English2 Wed Science3 Thu Music4 Fri GamesModified DataFrame: Day Subject0 mon Math1 tue English2 wed Science3 thu Music4 fri Games
Example 3
Let's see another example where we applySeries.str.casefold() in a more complex scenario −
import pandas as pd# Create a DataFrame with mixed-case textdf = pd.DataFrame({'Name': ['Alice', 'Bob', 'CHARLIE', 'david'], 'Role': ['Admin', 'user', 'MANAGER', 'staff']})# Print the original DataFrameprint("Original DataFrame:")print(df)# Apply casefold to both 'Name' and 'Role' columnsdf = df.apply(lambda x: x.str.casefold() if x.dtype == "object" else x)# Print the modified DataFrameprint("Modified DataFrame:")print(df)Output of the above code is as follows −
Original DataFrame: Name Role0 Alice Admin1 Bob user2 CHARLIE MANAGER3 david staffModified DataFrame: Name Role0 alice admin1 bob user2 charlie manager3 david staff