convert list to string using join

Question:

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

Asked By: Jutika Patil

||

Answers:

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
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.