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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42273937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:59:46  来源:igfitidea点击:

How to add string to all values in a column of pandas DataFrame?

python-3.xpandas

提问by mystery man

Say you have a DataFramewith columns;

假设你有一个DataFrame列;

 col_1    col_2 
   1        a
   2        b
   3        c
   4        d
   5        e

how would you change the values of col_2so that, new value= current value+ 'new'

您将如何更改的值,col_2以便新值=当前值+'new'

回答by jezrael

Use +:

使用+

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

Thanks hooyfor another solution:

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

df.col_2 += 'new'

Or assign:

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