Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.len() Method



TheSeries.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 PandasSeries.str.len() method:

Series.str.len()

Parameters

TheSeries.str.len() method does not accept any parameters.

Returns

TheSeries.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 theSeries.str.len() method to compute the length of string elements in a Series.

import pandas as pd# Create a Series of stringsseries = pd.Series(["Foo", "bar", "London", "Quarantine"])print("Series: \n", series)# Compute the length of each string elementlength = series.str.len()print("Length:\n", length)

When we run the above code, it produces the following output:

Series:  0           Foo1           bar2        London3    Quarantinedtype: objectLength: 0    31    32    63    10dtype: int64

Example 2

This example demonstrates theSeries.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 elementss = pd.Series(['panda', '', 5, {'language': 'Python'}, [2, 3, 5, 7], ('one', 'two', 'three')])print("Original Series: \n", s)# Compute the length of each elementlength = 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                     panda1                          2                         53    {'language': 'Python'}4              [2, 3, 5, 7]5         (one, two, three)dtype: objectOutput Length of each element in the Series: 0    5.01    0.02    NaN3    1.04    4.05    3.0dtype: float64

Example 3

This example demonstrates the use of theSeries.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 listsdf = 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         B0  [1, 2, 3]  Tutorial1  [0, 1, 3]     AEIOUDataFrame after computing the length of elements in column 'B':           A         B  C0  [1, 2, 3]  Tutorial  81  [0, 1, 3]     AEIOU  5
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp