Pandas groupby with delimiter join

2019-01-04 14:58发布

I tried to use groupby to group rows with multiple values.

col val
A  Cat
A  Tiger
B  Ball
B  Bat

import pandas as pd
df = pd.read_csv("Inputfile.txt", sep='\t')
group = df.groupby(['col'])['val'].sum()

I got

A CatTiger
B BallBat

I want to introduce a delimiter, so that my output looks like

A Cat-Tiger
B Ball-Bat

I tried,

group = df.groupby(['col'])['val'].sum().apply(lambda x: '-'.join(x))

this yielded,

A C-a-t-T-i-g-e-r
B B-a-l-l-B-a-t

What is the issue here ?

Thanks,

AP

2条回答
在下西门庆
2楼-- · 2019-01-04 15:23

Alternatively you can do it this way:

In [48]: df.groupby('col')['val'].agg('-'.join)
Out[48]:
col
A    Cat-Tiger
B     Ball-Bat
Name: val, dtype: object
查看更多
Viruses.
3楼-- · 2019-01-04 15:28

just try

group = df.groupby(['col'])['val'].apply(lambda x: '-'.join(x))
查看更多
登录 后发表回答