Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas - convert string into list of strings [duplicate]

Tags:

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' 
like image 989
Fabio Lamanna Avatar asked Aug 18 '17 14:08

Fabio Lamanna


People also ask

How do I convert a string to a list in pandas?

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 do you make each letter of a string into a list?

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.

How do you split a string into a list in Python?

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.


2 Answers

You can split the string manually:

>>> df['Tags'] = df.Tags.apply(lambda x: x[1:-1].split(',')) >>> df.Tags[0] ['Tag1', 'Tag2'] 
like image 108
Mike Müller Avatar answered Oct 14 '22 09:10

Mike Müller


Or

df.Tags=df.Tags.str[1:-1].str.split(',').tolist() 
like image 40
BENY Avatar answered Oct 14 '22 08:10

BENY