1

I have a simple dataset:

enter image description here

I want to compress the feature text column in ',' by animal, so my output will look like:

enter image description here

I appreciate any ideas on it. Thank you

Qianru Song
  • 331
  • 1
  • 4
  • 16
  • 1
    Is this pandas? Or just list? A simple dictionary? Can't help if you don't provide what you're using. Ideally, show us some code. – Faboor Mar 05 '21 at 14:20
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Mar 05 '21 at 14:29
  • Thank you. I will improve my question next time. Sorry for that. – Qianru Song Mar 05 '21 at 14:31

1 Answers1

1

You can use a groupby:

>>> df.groupby('animal')['Feature'].apply(','.join)
animal
bird    B,C
cat     A,C
Name: Feature, dtype: object

Note: for reproducible setup (preferred to an image):

df = pd.DataFrame({
    'animal': ['cat', 'cat', 'bird', 'bird'],
    'Feature': list('ACBC'),
})
Pierre D
  • 24,012
  • 7
  • 60
  • 96