I have this 'file.csv' file to read with 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 that the column Tags
is a full string, since:
In [64]: df['Tags'][0][0] Out[64]: '['
I need to read it as a list of strings like ["Tag1","Tag2"]
. I tried the solution provided in this question but no luck there, since I have the [
and ]
characters that actually mess up the things.
The expecting output should be:
In [64]: df['Tags'][0][0] Out[64]: 'Tag1'
You can convert the string to a list using strip and split . Show activity on this post. Your df['Tags'] appears to be a list of strings.
How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.
Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
You can split the string manually:
>>> df['Tags'] = df.Tags.apply(lambda x: x[1:-1].split(',')) >>> df.Tags[0] ['Tag1', 'Tag2']
Or
df.Tags=df.Tags.str[1:-1].str.split(',').tolist()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With