3

Sample data:

#importing libraries
import pandas as pd
from ast import literal_eval

#sample data
data=['{"level":0,"side":"Ask","price":"13745.75000","volume":"2"}',
     '{"level":1,"side":"Ask","price":"13745.50000","volume":"5"}',
     '{"level":2,"side":"Ask","price":"13745.25000","volume":"6"}',
     '{"level":3,"side":"Ask","price":"13745.00000","volume":"15"}',
     '{"level":4,"side":"Ask","price":"13744.75000","volume":"4"}']

ds=pd.Series(data)

So now I wanted to convert the string values back to it's original form(dictionary) so for this I used:

ds.apply(lambda x:eval(x))    #I know using eval is a bad practice
#OR
ds.apply(lambda x:literal_eval(x))

The above code(both methods) is giving me my expected output.

But If I use pd.eval() method:

ds.apply(pd.eval)
#OR
ds.apply(lambda x:pd.eval(x))

It is giving me NotImplementedError:

NotImplementedError: 'Dict' nodes are not implemented

My question is:

How can I use pd.eval() method for doing this?

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41

1 Answers1

1

I think pandas.eval is used mainly for arithmetic operations in pandas, so cannot be used for parsing strings repr of dictionaries like ast.literal_eval.

For more information check this

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252