在pandas中如何为一个列中的每个值添加后缀?

huangapple go评论133阅读模式
英文:

How do I add a suffix to every value in one column in pandas

问题

如何在pandas中将前缀更改为后缀?

英文:

I keep trying to add a suffix to every single value in one column in a dataframe. I accidentally added what I wanted to be a suffix as a prefix. I don't know how to fix this. How do I change the prefix to a suffix in pandas?

答案1

得分: 1

如果您的前缀对于每一列都相同且具有固定长度,则可以使用df[column].str[prefix_length:]将其修剪掉。例如,您的前缀是'_asdf',所以您想要从列中修剪掉前四个字符,可以使用df[column].str[4:]

要添加后缀,您可以使用df[column] = df[column] + suffix

英文:

If your prefix is the same for every column and thus has a fixed length, you can trim that off using df[column].str[prefix_length:]. E.g. your prefix is '_asdf', so you want to trim off the first four characters from your column with df[column].str[4:].

To add a suffix, you can use df[column] = df[column] + suffix

答案2

得分: 0

你可以使用 str 访问器将前缀转换为后缀。

假设以下数据框:

>>> df
           col1
0  _suffixHello
1  _suffixWorld

使用 str.replace

df['col1'] = df['col1'].str.replace(r'(_suffix)(.*)', r'', regex=True)
print(df)

# 输出
           col1
0  Hello_suffix
1  World_suffix

或者使用 str.slice

suffix = '_suffix'
df['col1'] = df['col1'].str.slice(len(suffix)) + suffix
# 与以下方式相同
# df['col1'].str[len(suffix):] + suffix
print(df)

# 输出
           col1
0  Hello_suffix
1  World_suffix
英文:

You can use str accessor to transform prefix as suffix.

Suppose the following dataframe:

>>> df
           col1
0  _suffixHello
1  _suffixWorld

With str.replace:

df['col1'] = df['col1'].str.replace(r'(_suffix)(.*)', r'', regex=True)
print(df)

# Output
           col1
0  Hello_suffix
1  World_suffix

Or using str.slice:

suffix = '_suffix'
df['col1'] = df['col1'].str.slice(len(suffix)) + suffix
# same as
# df['col1'].str[len(suffix):] + suffix
print(df)

# Output
           col1
0  Hello_suffix
1  World_suffix

答案3

得分: 0

# 让我们首先创建一个包含8个随机选择的颜色的系列:

colors = np.array(['blue', 'brown', 'black', 'cyan', 'green', 'maroon', 'magenta', 'orange', 'pink', 'purple', 'red', 'teal', 'yellow'])

s = pd.Series(np.random.choice(a = colors, size = 8, replace = False))

# 将一个你喜欢的后缀字符串分配给变量 'suffix':

# 然后使用 apply 方法和一个 lambda 函数将你的后缀连接到系列中的每个元素上。确保你系列/列中的所有元素的数据类型都是字符串。如果它们是数字,先将它们转换为字符串,否则会因为数据类型不匹配而产生错误。
英文:

Let's first create a series to work with of 8 randomly chosen colors:

colors = np.array(['blue','brown', 'black', 'cyan', 'green', 'maroon', 'magenta', 'orange','pink', 'purple', 'red', 'teal','yellow' ])

s = pd.Series(np.random.choice(a = colors, size = 8, replace = False))

在pandas中如何为一个列中的每个值添加后缀?

Assign a string that you'd like for your suffix to the variable, 'suffix':

Then use the apply method with a lambda function to concatenate your suffix to each element in the series. Make sure the dtypes of all your elements in your series/column are strings. If they're numbers, convert them to a str first, otherwise, you'll get an error because of mis-aligned dtypes.

在pandas中如何为一个列中的每个值添加后缀?

huangapple
  • 本文由 发表于 2023年6月22日 04:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定