convert list to string using join
Question:
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
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