1
x = [{'list1':'[1,6]', 'list2':'[1,1]'},
     {'list1':'[1,7]', 'list2':'[1,2]'}]

df = pd.DataFrame(x)

Now I'm going to transform it from string to list type:

df[['list1','list2']].apply(lambda x: ast.literal_eval(x.strip()))

>> ("'Series' object has no attribute 'strip'", 'occurred at index list1')

So I get an error, but if I single out only 1 column:

d['list1'].apply(lambda x: ast.literal_eval(x.strip()))

>>    0    [1, 6]
      1    [1, 7]
      Name: list1, dtype: object

Why is this happening? Why does it only allow one column instead of multiple?

Chipmunkafy
  • 566
  • 2
  • 5
  • 17

1 Answers1

2

It is important to understand how apply is supposed to work in order to understand why it isn't working for you. Each column (considering the default axis=0) is iteratively operated upon, you can see how by letting each series print itself:

df.apply(lambda x: print(x))
0    [1,6]
1    [1,7]
Name: list1, dtype: object
0    [1,1]
1    [1,2]
Name: list2, dtype: object

And when you try and call (series_object).strip(), the error makes more sense.


Since you want to apply your function to each cell individually, you can use applymap instead, it's relatively faster in comparison.

df[['list1','list2']].applymap(ast.literal_eval)

Or,

df[['list1','list2']].applymap(pd.eval)

    list1   list2
0  [1, 6]  [1, 1]
1  [1, 7]  [1, 2]

Other options also include:

df.apply(lambda x: x.map(ast.literal_eval))

    list1   list2
0  [1, 6]  [1, 1]
1  [1, 7]  [1, 2]

Among others.

cs95
  • 379,657
  • 97
  • 704
  • 746