简体   繁体   中英

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

Here's one way.

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

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM