Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.join() Method



TheSeries.str.join() method in Pandas is used for handling text data within a Series/Index or a column of a DateFrame. This method is particularly useful when dealing with lists contained within the elements of a Series.

With the specified delimiter, theSeries.str.join() method allows you to concatenate the contents of these lists into a single string. This operation is equivalent to the standard Pythonstr.join() method, but it is applied element-wise to each entry in the Series.

Syntax

Following is the syntax of the PandasSeries.str.join() method −

Series.str.join(sep)

Parameters

TheSeries.str.join() method accepts the following parameter −

  • sep − A string representing the delimiter to use between list entries.

Return Value

TheSeries.str.join() method returns a Series or Index of objects where the list entries are concatenated by intervening occurrences of the delimiter.

Raises

The method raises anAttributeError if the supplied Series contains neither strings nor lists.

Notes − If any of the list items is not a string object, the result of the join will be NaN.

Example 1

This example demonstrates joining lists contained as elements in a Series using theSeries.str.join() method.

import pandas as pd# Create a Series of listss = pd.Series([['a', 'b', 'c'], ['1', '2', '3'], ['x', 'y', 'z']])# Join the list entries with a comma delimiterresult = s.str.join(',')print("Input Series:")print(s)print("\nJoined Strings:")print(result)

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

Input Series:0    [a, b, c]1    [1, 2, 3]2    [x, y, z]dtype: objectJoined Strings:0    a,b,c1    1,2,32    x,y,zdtype: object

Example 2

This example demonstrates the behavior of theSeries.str.join() method when the elements in the Series are not lists.

import pandas as pd# Create a Series of stringss = pd.Series(['apple', 'banana', 'cherry'])# Attempt to join the string entries with a dash delimiterresult = s.str.join('-')print("Joined Strings:")print(result)

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

Joined Strings:0      a-p-p-l-e1    b-a-n-a-n-a2    c-h-e-r-r-ydtype: object

TheAttributeError is raised because the elements in the Series are not lists.

Example 3

This example demonstrates the behavior of theSeries.str.join() method when the list contains non-string objects.

import pandas as pd# Create a Series of lists with non-string objectss = pd.Series([['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z']])# Join the list entries with a comma delimiterresult = s.str.join(',')print("Input Series:")print(s)print("\nJoined Strings:")print(result)

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

Input Series:0    [a, b, c]1       [1, 2, 3]2    [x, y, z]dtype: objectJoined Strings:0    a,b,c1       NaN2    x,y,zdtype: object

A value ofNaN indicates that the list contains non-string objects.

python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp