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



str.join() Pandas str.join() is used to combine all items in the list, presented in a series with a passed delimiter. Since strings are also an array of characters (or a list of characters), therefore, when this method is applied to a series of strings, the string is concatenated at each character with the passed delimiter.

.str must be prefixed each time before calling this method to distinguish it from the default Python 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 you are using, click here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.

Example # 1 : joining string elements

This example uses the str.join () method in the Series of String column. As discussed earlier, a string is also an array of characters, and therefore each character in the string will be concatenated with the supplied delimiter using the str.join () method.

# pandas module import

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

  
# remove null columns to avoid errors 

data.dropna (inplace = True )

 
# string append and overwrite

data [ "Name" ] = data [ " Name " ]. str . join ( " - " )

  
# display
data

Output:
As shown in the output image, the line in the name column has been concatenated symbolically with a missing delimiter. 

Example # 2: Combining List Items

This example applies the str.join () method to a series of lists. The Data column in the command is split into a list using the

# pandas module import

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# remove null columns to avoid ь errors

data.dropna (inplace = True )

 
# line splitting and rewriting

data [ "Team" ] = data [ "Team" ]. str . split ( "t" )

 
# connects with & quot; _ & quot;

data [ " Team " ] = data [ "Team " ]. str  . join ( "_" )

 
# display
data

Output:
How shown in the output images, the data was split into a list using str.split () and then the list was concatenated using str.join () delimited by "_".

Dataframe after splitting —

DataFrame after joining the list & # 8212 ;