
- 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.len() Method
The Series.str.len() method in Python Pandas library is useful for calculating the length of each element in a Series or Index. The elements can be sequences such as strings, tuples, or lists, as well as collections like dictionaries.
This method is particularly useful for text data processing and analysis, as it allows you for quick and efficient calculation of the size or length of the elements within the Series.
Syntax
Following is the syntax of the Pandas Series.str.len() method:
Series.str.len()
Parameters
The Series.str.len() method does not accept any parameters.
Returns
The Series.str.len() method returns a Series or Index of integers. Each integer value indicates the length of the corresponding element in the original Series or Index.
Example 1
This example demonstrates the basic usage of the Series.str.len() method to compute the length of string elements in a Series.
import pandas as pd # Create a Series of strings series = pd.Series(["Foo", "bar", "London", "Quarantine"]) print("Series: \n", series) # Compute the length of each string element length = series.str.len() print("Length:\n", length)
When we run the above code, it produces the following output:
Series: 0 Foo 1 bar 2 London 3 Quarantine dtype: object Length: 0 3 1 3 2 6 3 10 dtype: int64
Example 2
This example demonstrates the Series.str.len() method with a Series containing different types of elements, including strings, empty strings, integers, dictionaries, lists, and tuples.
import pandas as pd # Create a Series with various types of elements s = pd.Series(['panda', '', 5, {'language': 'Python'}, [2, 3, 5, 7], ('one', 'two', 'three')]) print("Original Series: \n", s) # Compute the length of each element length = s.str.len() print("Output Length of each element in the Series:\n", length)
When we run the above code, it produces the following output:
Original Series: 0 panda 1 2 5 3 {'language': 'Python'} 4 [2, 3, 5, 7] 5 (one, two, three) dtype: object Output Length of each element in the Series: 0 5.0 1 0.0 2 NaN 3 1.0 4 4.0 5 3.0 dtype: float64
Example 3
This example demonstrates the use of the Series.str.len() method with a DataFrame containing lists in one of its columns. The length of each list is computed and stored in a new column.
import pandas as pd # Create a DataFrame with a column of lists df = pd.DataFrame({"A": [[1, 2, 3], [0, 1, 3]], "B": ['Tutorial', 'AEIOU']}) print("Original DataFrame:") print(df) # Compute the length of each element in column 'B' df['C'] = df['B'].str.len() print("\nDataFrame after computing the length of elements in column 'B':") print(df)
When we run the above code, it produces the following output:
Original DataFrame: A B 0 [1, 2, 3] Tutorial 1 [0, 1, 3] AEIOU DataFrame after computing the length of elements in column 'B': A B C 0 [1, 2, 3] Tutorial 8 1 [0, 1, 3] AEIOU 5