且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将字符串添加到 Pandas DataFrame 列中的所有值

更新时间:2022-11-01 21:52:26

使用+:

df.col_2 = df.col_2 + 'new'
print (df)
   col_1 col_2
0      1  anew
1      2  bnew
2      3  cnew
3      4  dnew
4      5  enew

感谢 hooy 提供另一种解决方案:

Thanks hooy for another solution:

df.col_2 += 'new'

或者 assign:

df = df.assign(col_2 = df.col_2 + 'new')
print (df)
   col_1 col_2
0      1  anew
1      2  bnew
2      3  cnew
3      4  dnew
4      5  enew