Search code examples
pandasstringlistpython-re

How to change string to list in pandas data frame?


I have data frame where one column has rows like this:

'["user1", "user2" ... ]'

and I want it to look like this:

["user1", "user2" ... ]

I want to do it this way, using strip(), but I do not know how to write " ' " in brancets.

df.col =  df.col.str.strip("")

Kindly help.


Solution

  • You can use eval function to achieve this. Given a string, eval parses it to python expression.

    Example:

    >>> str_items = '["user1", "user2"]'
    >>> items = eval(str_items)
    >>> type(items)
    <class 'list'>
    

    From the example we can see that the string expression is converted to python list.

    To apply eval to all items in column of a data frame we can use apply function.

    >>> import pandas as pd
    >>> frame = pd.DataFrame()
    >>> frame["items"] = [ '["user1", "user2"]', '["user1", "user2"]']
    

    Now I have constructed a data frame similar to yours. Only thing remaining is that we need to convert all items in frame column to list.

    >>> frame["items"] = frame["items"].apply(eval)
    >>> type(frame['items'][0])
    <class 'list'>