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

后端 未结 5 738
梦如初夏
梦如初夏 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:16

    Another solution with .loc:

    df = pd.DataFrame({'col': ['a', 0]})
    df.loc[df.index, 'col'] = 'string' + df['col'].astype(str)
    

    This is not as quick as solutions above (>1ms per loop slower) but may be useful in case you need conditional change, like:

    mask = (df['col'] == 0)
    df.loc[mask, 'col'] = 'string' + df['col'].astype(str)
    
    0 讨论(0)
  • 2020-11-28 03:17

    As an alternative, you can also use an apply combined with format (or better with f-strings) which I find slightly more readable if one e.g. also wants to add a suffix or manipulate the element itself:

    df = pd.DataFrame({'col':['a', 0]})
    
    df['col'] = df['col'].apply(lambda x: "{}{}".format('str', x))
    

    which also yields the desired output:

        col
    0  stra
    1  str0
    

    If you are using Python 3.6+, you can also use f-strings:

    df['col'] = df['col'].apply(lambda x: f"str{x}")
    

    yielding the same output.

    The f-string version is almost as fast as @RomanPekar's solution (python 3.6.4):

    df = pd.DataFrame({'col':['a', 0]*200000})
    
    %timeit df['col'].apply(lambda x: f"str{x}")
    117 ms ± 451 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    %timeit 'str' + df['col'].astype(str)
    112 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    

    Using format, however, is indeed far slower:

    %timeit df['col'].apply(lambda x: "{}{}".format('str', x))
    185 ms ± 1.07 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-28 03:30

    You can use pandas.Series.map :

    df['col'].map('str{}'.format)
    

    It will apply the word "str" before all your values.

    0 讨论(0)
  • 2020-11-28 03:32

    If you load you table file with dtype=str
    or convert column type to string df['a'] = df['a'].astype(str)
    then you can use such approach:

    df['a']= 'col' + df['a'].str[:]
    

    This approach allows prepend, append, and subset string of df.
    Works on Pandas v0.23.4, v0.24.1. Don't know about earlier versions.

    0 讨论(0)
提交回复
热议问题