Open In App
Related Articles

Python | Pandas str.join() to join string/list elements with passed delimiter

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

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

Syntax: Series.str.join(sep)

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




# importing pandas module 
import pandas as pd
     
# reading csv file from url 
  
# dropping null value columns to avoid errors
data.dropna(inplace = True)
    
# joining string and overwriting 
data["Name"]= data["Name"].str.join("-")
  
# display
data


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




# importing pandas module 
import pandas as pd
     
# reading csv file from url 
  
# dropping null value columns to avoid errors
data.dropna(inplace = True)
    
# splitting string and overwriting 
data["Team"]= data["Team"].str.split("t")
  
# joining with "_"
data["Team"]= data["Team"].str.join("_")
  
# display
data


Output:
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 –


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Sep, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials