Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removing square brackets in creating a new column pandas

I have a pandas df, something like this:

     col1              col2             
     ABC       [hello, hi, hey, hiya]

my task is to extract the first three words of col2 into a new column with a hyphen in between. Something like this:

     col1              col2                 col3    
     ABC       [hello, hi, hey, hiya]    hello-hi-hey

this seemed simple enough, but I am not able to remove the square brackets anyway I try in new column. Is this possible to do? Any help will be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming a Series of lists, slice and join:

df['col3'] = df['col2'].str[:3].agg('-'.join)

If you rather have string representations of lists:

import re
df['col3'] = ['-'.join(re.split(', ', s[1:-1])[:3]) for s in df['col2']]

output:

  col1                    col2          col3
0  ABC  [hello, hi, hey, hiya]  hello-hi-hey
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading