Sean
Sean

Reputation: 47

What is the best way to convert a string in a pandas dataframe to a list?

Basically I have a dataframe with lists that have been read in as strings and I would like to convert them back to lists.

Below shows what I am currently doing but I m still learning and feel like there must be a better (more efficient/Pythonic) way to go about this. Any help/constructive criticism would be much appreciated!

import pandas as pd
import ast

df = pd.DataFrame(data=['[-1,0]', '[1]', '[1,2]'], columns = ['example'])
type(df['example'][0])
>> str

n = df.shape[0]
temp = []
temp2 = []

for i in range(n):
    temp = (ast.literal_eval(df['example'][i]))
    temp2.append(temp)

df['new_col_lists'] = temp2
type(df['new_col_lists'][0])
>> list

Upvotes: 3

Views: 126

Answers (3)

SpghttCd
SpghttCd

Reputation: 10890

You could use apply with a lambda which splits and converts your strings:

df['new_col_lists'] = df['example'].apply(lambda s: [int(v.strip()) for v in s[1:-1].split(',')])

Use float cast instead of int if needed.

Upvotes: 0

Rakesh
Rakesh

Reputation: 82795

You can use .apply

Ex:

import pandas as pd
import ast

df = pd.DataFrame(data=['[-1,0]', '[1]', '[1,2]'], columns = ['example'])
df['example'] = df['example'].apply(ast.literal_eval)
print( type(df['example'][0]) )

Output:

<type 'list'>

Upvotes: 2

kevh
kevh

Reputation: 323

Maybe you could use a map:

df['example'] = df['example'].map(ast.literal_eval)

With pandas, there is almost always a way to avoid the for loop.

Upvotes: 3

Related Questions