
- 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.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.