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 )
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.
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 Output: Dataframe after splitting — DataFrame after joining the list & # 8212 ;
# pandas module import
pandas as pd
# read CSV file from URL
" https://media.python.engineering/wp-content/uploads/nba.csv "
# remove null columns to avoid ь errors
# line splitting and rewriting
. split (
# connects with & quot; _ & quot;
" Team "
. join (
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 ;