
- 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
TheSeries.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
TheSeries.str.cat() method returns a concatenated string ifothers 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 theSeries.str.cat() method.
import pandas as pdimport numpy as np# Create a Seriess = 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 a1 b2 NaN3 ddtype: objectOutput: abd
Example 2
This example replaces the missing values with the given a representation using the using "na_rep" parameter.
import pandas as pdimport numpy as np# Create a Seriess = pd.Series(['a', 'b', np.nan, 'd'])print('Input Series:')print(s)# Concatenate with na_represult = 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 pdimport numpy as np# Create a Seriess = 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 a1 b2 NaN3 ddtype: objectOutput: 0 a,A1 b,B2 NaN3 d,Ddtype: object
Example 4
Following example demonstrates how to concatenate two Series with different indexes using the "join" keyword.
import pandas as pdimport numpy as np# Create Series with different indexess = 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 aa1 b-2 -c3 dddtype: objectjoin='outer':0 aa1 b-2 -c3 dd4 -edtype: objectjoin='inner':0 aa2 -c3 dddtype: objectjoin='right':3 dd0 aa4 -e2 -cdtype: object
Example 5
Let's look at another example of demonstrating the working of theSeries.str.cat() method on the Pandas DataFrame columns.
import pandas as pd# Read the data into a DataFramedata = {'Name': ['John', 'Jane', 'Alice'],'Surname': ['Doe', 'Smith', 'Johnson']}df = pd.DataFrame(data)# Display the input DataFrameprint("Original DataFrame:")print(df)# Join the columnsdf['Full Name'] = df['Name'].str.cat(df['Surname'], sep=' ')# Display the joined dataprint('Output Modified DataFrame:')print(df)When we run the above program, it produces the following result −
Original DataFrame: Name Surname0 John Doe1 Jane Smith2 Alice JohnsonOutput Modified DataFrame:Name Surname Full Name0 John Doe John Doe1 Jane Smith Jane Smith2 Alice Johnson Alice Johnson