Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a string to int/float dataframes in Pandas

This is my original dataframe:

  >>> df
           c0      c1      c2        c3   c4   c5   c6  c7     c8
    0  key0:j  key1:z  key2:b  key3:bsy  afj  upz  343  13  ruhwd
    1  key0:u  key1:i  key2:a  key3:dvp  ibt  dxv  154   0  adsif
    2  key0:t  key1:a  key2:a  key3:jqj  dtd  yxq  540  49  kxthz
    3  key0:j  key1:z  key2:b  key3:bsy  afj  upz  322  13  ruhwd
    4  key0:j  key1:z  key2:b  key3:bsy  afj  upz  397  13  ruhwd
    5  key0:u  key1:i  key2:a  key3:dvp  ibt  dxv  110   0  adsif
    6  key0:t  key1:a  key2:a  key3:jqj  dtd  yxq  526  49  kxthz
    7  key0:t  key1:u  key2:g  key3:nfk  ekh  trc   85  83  xppnl

I am calculating sum of c6 for c0 in pandas using this:

df.groupby(['c0'])['c6'].sum().reset_index()

Output:

       c0     0
0  key0:j  1062
1  key0:t  1151
2  key0:u   264

Here, I want the output of the groupby in such a way, that the column with sum should also have a string 'abc' attached to it. Like below:

    c0        0
0  key0:j     abc1062
1  key0:t     abc1151
2  key0:u     abc264

I went through a lot of posts, and could not get the desired answer. Any help will be appreciated.

like image 938
Mayank Porwal Avatar asked Feb 01 '26 11:02

Mayank Porwal


1 Answers

You could apply lambda after grouping before reset_index:

In [19]: df.groupby(['c0'])['c6'].sum().apply(lambda x: 'abc' + x.astype(str)).reset_index()
Out[19]:
       c0       c6
0  key0:j  abc1062
1  key0:t  abc1151
2  key0:u   abc264

Or you could use groupby as_index argument and then check for your col name c6:

In [29]: df.groupby(['c0'], as_index=False)['c6'].sum().apply(lambda x: 'abc' + x.astype(str) if x.name=='c6' else x)
Out[29]:
       c0       c6
0  key0:j  abc1062
1  key0:t  abc1151
2  key0:u   abc264
like image 185
Anton Protopopov Avatar answered Feb 03 '26 01:02

Anton Protopopov