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