Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp