在pandas数据框中填充一列字符串

时间:2022-11-13 15:51:18

I have a pandas dataframe with string in a column like this

我有一个像这样的列中带有字符串的pandas数据帧

 id   b
  1   this is string1
  1   this is string2
  1   this is string3
  1   this is string4

Now I want to remove the first character(t) from each cell of column b. I also need to add a character s at the beginning and end of this column. So the output should look like this

现在我想从列b的每个单元格中删除第一个字符(t)。我还需要在本专栏的开头和结尾添加一个字符。所以输出应该是这样的

 id   b
  1   shis is string1s
  1   shis is string2s
  1   shis is string3s
  1   shis is string4s

I know I can iterate over each row and do these operations but I was hopping there might be some efficient way of doing this. Maybe I can apply same operation on all cells of column b at once?

我知道我可以遍历每一行并执行这些操作,但我正在跳跃可能有一些有效的方法来做到这一点。也许我可以同时对b列的所有单元格应用相同的操作?

3 个解决方案

#1


4  

A more concise/flexible approach with df.apply:

使用df.apply更简洁/灵活的方法:

df.b = df.b.str[1:].apply('s{}s'.format)
print(df)

   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

And, to replace just the first occurrence of t, use pd.Series.str.replace:

并且,要仅替换第一次出现的t,请使用pd.Series.str.replace:

df.b = df.b.str.replace('t', '', 1).apply('s{}s'.format)
print(df)

   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

#2


3  

In [6]: df.b = 's' + df.b.str[1:] + 's'

In [7]: df
Out[7]:
   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

if you want to replace a first occurance of t:

如果你想要替换第一次出现的t:

In [14]: df
Out[14]:
   id              b
0   1           test
1   2         a test
2   3  no occurences

In [15]: df.b = df.b.str.replace('t', '-', n=1)

In [16]: df
Out[16]:
   id              b
0   1           -est
1   2         a -est
2   3  no occurences

#3


0  

You can replace the first and last character like this:

你可以像这样替换第一个和最后一个字符:

df['b'] = df.b.str.replace('^.|$', 's')

#1


4  

A more concise/flexible approach with df.apply:

使用df.apply更简洁/灵活的方法:

df.b = df.b.str[1:].apply('s{}s'.format)
print(df)

   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

And, to replace just the first occurrence of t, use pd.Series.str.replace:

并且,要仅替换第一次出现的t,请使用pd.Series.str.replace:

df.b = df.b.str.replace('t', '', 1).apply('s{}s'.format)
print(df)

   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

#2


3  

In [6]: df.b = 's' + df.b.str[1:] + 's'

In [7]: df
Out[7]:
   id                 b
0   1  shis is string1s
1   1  shis is string2s
2   1  shis is string3s
3   1  shis is string4s

if you want to replace a first occurance of t:

如果你想要替换第一次出现的t:

In [14]: df
Out[14]:
   id              b
0   1           test
1   2         a test
2   3  no occurences

In [15]: df.b = df.b.str.replace('t', '-', n=1)

In [16]: df
Out[16]:
   id              b
0   1           -est
1   2         a -est
2   3  no occurences

#3


0  

You can replace the first and last character like this:

你可以像这样替换第一个和最后一个字符:

df['b'] = df.b.str.replace('^.|$', 's')