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

前端 未结 1 518
迷失自我
迷失自我 2021-01-20 09:11

Say you have a DataFrame with columns;

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

相关标签:
1条回答
  • 2021-01-20 09:45

    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 hooy for another solution:

    df.col_2 += 'new'
    

    Or 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
    
    0 讨论(0)
提交回复
热议问题