python - pandas - convert string into list of strings -
i have 'file.csv' file read pandas:
title|tags t1|"[tag1,tag2]" t1|"[tag1,tag2,tag3]" t2|"[tag3,tag1]" using
df = pd.read_csv('file.csv', sep='|') the output is:
title tags 0 t1 [tag1,tag2] 1 t1 [tag1,tag2,tag3] 2 t2 [tag3,tag1] i know column tags full string, since:
in [64]: df['tags'][0][0] out[64]: '[' i need read list of strings ["tag1","tag2"]. tried solution provided in this question no luck there, since have [ , ] characters mess things.
the expecting output should be:
in [64]: df['tags'][0][0] out[64]: 'tag1'
you can split string manually:
>>> df['tags'] = df.tags.apply(lambda x: x[1:-1].split(',')) >>> df.tags[0] ['tag1', 'tag2']
Comments
Post a Comment