Pandas Series.str.cat() Method



The Series.str.cat() method in Pandas is used to concatenate strings in a Series or Index with a given separator. This method can concatenate the Series/Index with elements from another Series, Index, DataFrame, NumPy array, or list-like objects. If no other elements are specified, it concatenates all values in the Series/Index into a single string with the given separator.

Syntax

Following is the syntax of the Pandas Series.str.cat() method −

Series.str.cat(others=None, sep=None, na_rep=None, join='left')

Parameters

The Pandas Series.str.cat() method accepts the following parameters −

  • others − Series, Index, DataFrame, np.ndarray, or list-like objects to be concatenated with the calling Series/Index. They must have the same length as the calling Series/Index, except for indexed objects when join is not None.

  • sep − The separator to be used between the concatenated elements. The default is an empty string ''.

  • na_rep − The representation for missing values. If None, missing values are omitted if others is None, otherwise rows with missing values in any columns before concatenation will have a missing value in the result.

  • join − Specifies the join style between the calling Series/Index and any Series/Index/DataFrame in others. Options are {'left', 'right', 'outer', 'inner'}. The default is 'left'.

Return Value

The Series.str.cat() method returns a concatenated string if others is None. Otherwise, it returns a Series/Index (same type as caller) of concatenated objects.

Example 1

Here is an basic example of concatenating the all values into a single string using the Series.str.cat() method.

import pandas as pd
import numpy as np

# Create a Series
s = pd.Series(['a', 'b', np.nan, 'd'])
print('Input Series:')
print(s)

# Concatenate without 'others'
result = s.str.cat()
print("Output:",result)

Following is the output of the above code −

Input Series:
0      a
1      b
2    NaN
3      d
dtype: object

Output: abd

Example 2

This example replaces the missing values with the given a representation using the using "na_rep" parameter.

import pandas as pd
import numpy as np

# Create a Series
s = pd.Series(['a', 'b', np.nan, 'd'])
print('Input Series:')
print(s)

# Concatenate with na_rep
result = s.str.cat(sep=' ', na_rep='?')
print("Output:",result)

Output of the above code is as follows −

'a b ? d'

Example 3

This example concatenates the input Series with "others" object.

import pandas as pd
import numpy as np

# Create a Series
s = pd.Series(['a', 'b', np.nan, 'd'])
print('Input Series:')
print(s)

# Concatenate with 'others'
result = s.str.cat(['A', 'B', 'C', 'D'], sep=',')
print("Output:",result)

The output of the above code is as follows −

Input Series:
0      a
1      b
2    NaN
3      d
dtype: object

Output: 
0    a,A
1    b,B
2    NaN
3    d,D
dtype: object

Example 4

Following example demonstrates how to concatenate two Series with different indexes using the "join" keyword.

import pandas as pd
import numpy as np

# Create Series with different indexes
s = pd.Series(['a', 'b', np.nan, 'd'])
t = pd.Series(['d', 'a', 'e', 'c'], index=[3, 0, 4, 2])

# Concatenate with 'join=left'
result_left = s.str.cat(t, join='left', na_rep='-')
print(result_left)

# Concatenate with 'join=outer'
result_outer = s.str.cat(t, join='outer', na_rep='-')
print(result_outer)

# Concatenate with 'join=inner'
result_inner = s.str.cat(t, join='inner', na_rep='-')
print(result_inner)

# Concatenate with 'join=right'
result_right = s.str.cat(t, join='right', na_rep='-')
print(result_right)

The output of the above code is as follows −

join='left':
0    aa
1    b-
2    -c
3    dd
dtype: object
join='outer':
0 aa
1 b-
2 -c
3 dd
4 -e
dtype: object

join='inner':
0 aa
2 -c
3 dd
dtype: object

join='right':
3 dd
0 aa
4 -e
2 -c
dtype: object

Example 5

Let's look at another example of demonstrating the working of the Series.str.cat() method on the Pandas DataFrame columns.

import pandas as pd

# Read the data into a DataFrame
data = {'Name': ['John', 'Jane', 'Alice'],
'Surname': ['Doe', 'Smith', 'Johnson']}
df = pd.DataFrame(data)

# Display the input DataFrame
print("Original DataFrame:")
print(df)

# Join the columns
df['Full Name'] = df['Name'].str.cat(df['Surname'], sep=' ')

# Display the joined data
print('Output Modified DataFrame:')
print(df)

When we run the above program, it produces the following result −

Original DataFrame:
    Name  Surname
0   John      Doe
1   Jane    Smith
2  Alice  Johnson
Output Modified DataFrame:
Name Surname Full Name
0 John Doe John Doe
1 Jane Smith Jane Smith
2 Alice Johnson Alice Johnson
python_pandas_working_with_text_data.htm
Advertisements