如何将字符串添加到pandas DataFrame列中的所有值? [英] How to add string to all values in a column of pandas DataFrame?
本文介绍了如何将字符串添加到pandas DataFrame列中的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有一个带有列的DataFrame
;
Say you have a DataFrame
with columns;
col_1 col_2
1 a
2 b
3 c
4 d
5 e
如何更改col_2
的值,以便新值 = 当前值 + 'new'
how would you change the values of col_2
so that, new value = current value + 'new'
推荐答案
使用+
:
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
这篇关于如何将字符串添加到pandas DataFrame列中的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文