Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
str.join() method is used to join all elements in list present in a series with passed delimiter. Since strings are also array of character (or List of characters), hence when this method is applied on a series of strings, the string is joined at every character with the passed delimiter.
.str has to be prefixed every time before calling this method to differentiate it from the Python’s default string method.
sep: string value, joins elements with the string between them
Return type: Series with joined elements
To download the Csv file used, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Example #1: Joining string elements
In this example, the str.join() method is used on Name column (Series of String). As discussed earlier, a string is also an array of character and hence every character of string will be joined with the passed separator using str.join() method.
As shown in the output image, the string in name column have been joined character wise with the passed separator.
Example #2: Joining elements of a list
In this example, the str.join() method is applied to a series of list. The Data in team column is separated into list using str.split() method.
As shown in the output images, the data was splitted into list using str.split() and then the list was joined using str.join() with separator “_”.
Dataframe after splitting –
DataFrame after joining list –
- How to read a CSV file to a Dataframe with custom delimiter in Pandas?
- Python - Segregate elements by delimiter
- Python - Concatenate Tuple elements by delimiter
- Python | Split on last occurrence of delimiter
- Python | Ways to split strings using newline delimiter
- Python - Replace delimiter
- Python - Convert delimiter seperated Mixed String to valid List
- Python - Convert List to delimiter separated String
- Python - Convert Delimiter separated list to Number
- Python - Construct dictionary Key-Value pairs separated by delimiter
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python - Sort words separated by Delimiter
- Python Program to perform cross join in Pandas
- Join two text columns into a single column in Pandas
- How to Join Pandas DataFrames using Merge?
- Python | Join tuple elements in a list
- Python | Ways to join pair of elements in list
- Python - Consecutive K elements join in List
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Python program to split and join a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.