add a string prefix to each value in a string column using Pandas

后端 未结 5 750
梦如初夏
梦如初夏 2020-11-28 03:05

I would like to append a string to the start of each value in a said column of a pandas dataframe (elegantly). I already figured out how to kind-of do this and I am currentl

5条回答
  •  臣服心动
    2020-11-28 03:29

    df['col'] = 'str' + df['col'].astype(str)
    

    Example:

    >>> df = pd.DataFrame({'col':['a',0]})
    >>> df
      col
    0   a
    1   0
    >>> df['col'] = 'str' + df['col'].astype(str)
    >>> df
        col
    0  stra
    1  str0
    

提交回复
热议问题