Pandas Series.str.cat() 方法



Pandas 中的Series.str.cat()方法用于连接 Series 或 Index 中的字符串,并使用给定的分隔符。此方法可以将 Series/Index 与来自另一个 Series、Index、DataFrame、NumPy 数组或列表状对象的元素连接起来。如果未指定其他元素,则会使用给定的分隔符将 Series/Index 中的所有值连接成单个字符串。

语法

以下是 Pandas Series.str.cat() 方法的语法:

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

参数

Pandas Series.str.cat() 方法接受以下参数:

  • others - Series、Index、DataFrame、np.ndarray 或列表状对象,将与调用的 Series/Index 连接。它们必须与调用的 Series/Index 具有相同的长度,除了当 join 不为 None 时索引对象。

  • sep - 连接元素之间使用的分隔符。默认为空字符串 ''。

  • na_rep - 缺失值的表示形式。如果为 None,则如果 others 为 None,则省略缺失值,否则在连接之前任何列中具有缺失值的行的结果将具有缺失值。

  • join - 指定调用 Series/Index 与 others 中的任何 Series/Index/DataFrame 之间的连接样式。选项包括 {'left', 'right', 'outer', 'inner'}。默认为 'left'。

返回值

如果others 为 None,则Series.str.cat()方法返回连接的字符串。否则,它返回连接对象的 Series/Index(与调用者相同类型)。

示例 1

这是一个使用Series.str.cat()方法将所有值连接成单个字符串的基本示例。

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)

以上代码的输出如下:

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

Output: abd

示例 2

此示例使用 "na_rep" 参数用给定的表示形式替换缺失值。

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)

以上代码的输出如下:

'a b ? d'

示例 3

此示例将输入 Series 与 "others" 对象连接。

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)

以上代码的输出如下:

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

示例 4

以下示例演示了如何使用 "join" 关键字连接两个具有不同索引的 Series。

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)

以上代码的输出如下:

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

示例 5

让我们再看一个示例,演示Series.str.cat()方法在 Pandas DataFrame 列上的工作原理。

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)

当我们运行以上程序时,它会产生以下结果:

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
广告

© . All rights reserved.