Reputation: 34056
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.
Upvotes: 3
Views: 1434
Reputation: 862691
You can use ix
for selecting second column and convert int
column to string
by astype
:
print df
c0 0
0 key0:j 1062
1 key0:t 1151
2 key0:u 264
df.ix[:,1] = '"abc"' + df.ix[:,1].astype(str)
print df
c0 0
0 key0:j "abc"1062
1 key0:t "abc"1151
2 key0:u "abc"264
Or:
df.ix[:,1] = 'abc' + df.ix[:,1].astype(str)
print df
c0 0
0 key0:j abc1062
1 key0:t abc1151
2 key0:u abc264
If columns are a
and b
:
print df
a b
0 key0:j 1062
1 key0:t 1151
2 key0:u 264
df['b'] = 'abc' + df['b'].astype(str)
print df
a b
0 key0:j abc1062
1 key0:t abc1151
2 key0:u abc264
EDIT:
You can try, but I think it is slower as solution above:
df = df.groupby(['c0'])['c6'].apply(lambda x: 'abc' + str( x.sum())).reset_index()
print df
c0 c6
0 key0:j abc1062
1 key0:t abc1151
2 key0:u abc264
Upvotes: 1
Reputation: 31672
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
Upvotes: 1