- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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