
- 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.capitalize() Method
TheSeries.str.capitalize() method in Pandas is used to capitalize the first character of each string in a Series or Index. This method is a convenient way to standardize the case of text data, ensuring that each string starts with an uppercase letter and the rest are in lowercase. This operation is similar to the string methodstr.capitalize() in Python.
Syntax
Following is the syntax of the PandasSeries.str.capitalize() method −
Series.str.capitalize()
Parameters
TheSeries.str.capitalize() method does not accept any parameters.
Return Value
TheSeries.str.capitalize() method returns a new Series with the first letter of each string capitalized and all other letters in lowercase.
Example 1
In this example, we demonstrate the basic usage of theSeries.str.capitalize() method by applying it to a Series of strings.
import pandas as pd# Create a Series of stringss = pd.Series(['hi,', 'welcome to', 'tutorialspoint'])# Display the input Seriesprint("Input Series")print(s)# Capitalize the first letter of each stringprint("Series after calling the Capitalize:")print(s.str.capitalize())When we run the above code, it produces the followingoutput −
Input Series0 hi,1 welcome to2 tutorialspointdtype: objectSeries after calling the Capitalize:0 Hi,1 Welcome to2 Tutorialspointdtype: object
Example 2
This example demonstrates how to use theSeries.str.capitalize() method to format the 'Day' column in a DataFrame, converting each day's name to proper capitalization.
import pandas as pd# Create a DataFramedf = pd.DataFrame({'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['Math', 'english', 'science', 'music', 'games']})print("Input DataFrame:")print(df)# Capitalize the first letter of each daydf.Day = df.Day.str.capitalize()print("DataFrame after applying Capitalize:")print(df)Following is theoutput of the above code −
Input DataFrame: Day Subject0 mon Math1 tue english2 wed science3 thu music4 fri gamesDataFrame after applying Capitalize: Day Subject0 Mon Math1 Tue english2 Wed science3 Thu music4 Fri games
Example 3
In this example, we apply theSeries.str.capitalize() method to an Index object. This showcases how you can use it to format the index labels in a DataFrame.
import pandas as pd# Create a DataFrame with an Indexdf = pd.DataFrame({'Value': [1, 2, 3]}, index=['first', 'second', 'third'])# Capitalize the first letter of each index labeldf.index = df.index.str.capitalize()print(df)Output of the above code is as follows −
ValueFirst 1Second 2Third 3