김수환
김수환

Reputation: 131

How to add a character to a specific column?

I have a pandas.DataFrame of the form. I'll show you a simple example.(In reality, it consists of hundreds of millions of rows of data.). I want to couple the 'a' before the character in column '1'.

df=

  index    1            2         3
    

    0      0           100       1
    1      1.04        100       2
    2      32          100       3
    3      5.05        105       4
    4      1.01        105       5
    5      155         105       6
    6      3155.26     105       7
    7      354.12      100       8
    8      5680.13     100       9
    9      125.55      100       10
    10     13.32       100       11
    11     5656.33     156       12
    12     456.61      156       13
    13     23.52       1235      14
    14     35.35       1235      15
    15     350.20      100       16
    16     30.         100       17
    17     13.50       100       18
    18     323.13      231       19
    19     15.11       1111      20
    20     11.22       1111      21

Here is my expected result:

df=

  index    1            2         3
    

    0      a0           100       1
    1      a1.04        100       2
    2      a32          100       3
    3      a5.05        105       4
    4      a1.01        105       5
    5      a155         105       6
    6      a3155.26     105       7
    7      a354.12      100       8
    8      a5680.13     100       9
    9      a125.55      100       10
    10     a13.32       100       11
    11     a5656.33     156       12
    12     a456.61      156       13
    13     a23.52       1235      14
    14     a35.35       1235      15
    15     a350.20      100       16
    16     a30.         100       17
    17     a13.50       100       18
    18     a323.13      231       19
    19     a15.11       1111      20
    20     a11.22       1111      21

How do I solve this problem?

Upvotes: 0

Views: 388

Answers (2)

jezrael
jezrael

Reputation: 863501

Use + for concanecate:

df[1] = 'a' + df[1].astype(str)

Or with f-strings:

df[1] = df[1].map(lambda x: f'a{x}')

Performance:

#[210000 rows x 3 columns]
df = pd.concat([df] * 10000, ignore_index=True)

In [116]: %timeit df[1].map(lambda x: f'a{x}')
160 ms ± 3.27 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [117]: %timeit 'a' + df[1].astype(str)
147 ms ± 5.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [118]: %timeit df[1].apply(lambda x: "a" + str(x))
162 ms ± 17.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Upvotes: 1

alphaBetaGamma
alphaBetaGamma

Reputation: 677

You can use lambda expression:

df[1] = df[1].apply(lambda x: "a" + str(x))

Upvotes: 1

Related Questions