J.Do J.Do - 3 years ago 76
Python Question

Is it possible to join all the same terms into the same pandas dataframe column?

I have the following large pandas dataframe, which is composed of several terms:

type name exp
-------------------
feline tiger True
feline cat False
rodent rabbit True
canine dog False
feline puma True
feline bobcat False


Is it possible to join all the terms in the
name
column that have the same type in the
type
column into the same cell?. For example:

type name exp
----------------------------------
feline tiger cat puma bobcat True
rodent rabbit True
canine dog False

Answer Source

Using df.groupby:

In [200]: df_grouped = df.groupby('type', sort=False, as_index=False) 

First handle name:

In [202]: df_grouped['name'].apply(lambda x: ' '.join(x))
Out[202]: 
0    tiger cat puma bobcat
1                   rabbit
2                      dog
dtype: object

Now, handle exp:

In [203]: df_grouped['exp'].apply(any)
Out[203]: 
0     True
1     True
2    False
dtype: bool

Putting it together:

In [219]: df_grouped = df.groupby('type', sort=False, as_index=False).agg({'name' : ' '.join, 'exp' : any}); df_grouped
Out[219]: 
     type                   name    exp
0  feline  tiger cat puma bobcat   True
1  rodent                 rabbit   True
2  canine                    dog  False

To retain unique items only, pass a lambda to name:

df.groupby('type', sort=False, as_index=False)\
       .agg({'name' : lambda x: ' '.join(set(x)), 'exp' : any})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download