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

convert list to string using join

I was trying to convert the below tags column to normal string without the brackets and commas and tried the below two ways but I’m getting error.
can someone tell a correct way

enter image description here

Error: can only join an iterable

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 :

If there are missing values and lists filled by strings, you can use Series.str.join:

new['tags'] = new['tags'].str.join(' ')

Or join only rows with no missing values:

m = new['tags'].notna()
new.loc[m, 'tags'] = new.loc[m, 'tags'].apply(' '.join)

If there are also non strings values in lists:

new = pd.DataFrame({'tags': [['In','ss',5], None]})
    
m = new['tags'].notna()
new.loc[m, 'tags'] = new.loc[m, 'tags'].apply(lambda x : ' '.join(map(str, x)))
print (new)
        tags
0  In, ss, 5
1       None
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