Pandas Series.str.join() Method



The Series.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, the Series.str.join() method allows you to concatenate the contents of these lists into a single string. This operation is equivalent to the standard Python str.join() method, but it is applied element-wise to each entry in the Series.

Syntax

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

Series.str.join(sep)

Parameters

The Series.str.join() method accepts the following parameter −

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

Return Value

The Series.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 an AttributeError 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 the Series.str.join() method.

import pandas as pd

# Create a Series of lists
s = pd.Series([['a', 'b', 'c'], ['1', '2', '3'], ['x', 'y', 'z']])

# Join the list entries with a comma delimiter
result = 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: object

Joined Strings:
0    a,b,c
1    1,2,3
2    x,y,z
dtype: object

Example 2

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

import pandas as pd

# Create a Series of strings
s = pd.Series(['apple', 'banana', 'cherry'])

# Attempt to join the string entries with a dash delimiter
result = 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-e
1    b-a-n-a-n-a
2    c-h-e-r-r-y
dtype: object

The AttributeError is raised because the elements in the Series are not lists.

Example 3

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

import pandas as pd

# Create a Series of lists with non-string objects
s = pd.Series([['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z']])

# Join the list entries with a comma delimiter
result = 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: object

Joined Strings:
0    a,b,c
1       NaN
2    x,y,z
dtype: object

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

python_pandas_working_with_text_data.htm
Advertisements