python - right way to use eval statement in pandas dataframe map function -
i have pandas dataframe 1 column 'organization', , content of such column string list inside string :
data['organization'][0] out[6] "['loony tunes']" data['organization'][1] out[7] "['the 3 stooges']"
i want substitute string list inside string. try use map, function inside map eval:
data['organization'] = data['organization'].map(eval)
but is:
traceback (most recent call last): file "c:\users\xxx\anaconda3\lib\site- packages\ipython\core\interactiveshell.py", line 3035, in run_code exec(code_obj, self.user_global_ns, self.user_ns) file "<ipython-input-7-3dbc0abf8c2e>", line 1, in <module> data['organization'] = data['organization'].map(eval) file "c:\users\xxx\anaconda3\lib\site-packages\pandas\core\series.py", line 2015, in map mapped = map_f(values, arg) file "pandas\src\inference.pyx", line 1046, in pandas.lib.map_infer (pandas\lib.c:56983) typeerror: eval() arg 1 must string, bytes or code object
thus resorted following block of code, extremely inefficient:
for index, line in data['organization'].iteritems(): print(index) if type(line) != str: data['organization'][index] = [] try: data['organization'][index] = eval(data['organization'][index]) except: continue
what doing wrong? how can use eval (or vectorized implementation) instead of clumsy loop above?
i thought problem might elements in pd.series data['organization'] not strings, implemented following:
def is_string(x): if type(x) != str: x = '' data['organization'] = data['organization'].map(is_string)
but still same error when try:
data['organization'] = data['organization'].map(eval)
thanks in advance.
using eval frowned upon allows arbitrary python code run. should strongly prefer not use if possible.
in case, don't need evaluate expression, need parse value. means can use ast's literal_eval
:
in [11]: s = pd.series(["['loony tunes']", "['the 3 stooges']"]) in [12]: ast import literal_eval in [13]: s.apply(literal_eval) out[13]: 0 [loony tunes] 1 [the 3 stooges] dtype: object in [14]: s.apply(literal_eval)[0] # look, works! out[14]: ['loony tunes']
from docs:
ast.literal_eval(node_or_string)
safely evaluate expression node or unicode or latin-1 encoded string containing python literal or container display. string or node provided may consist of following python literal structures: strings, numbers, tuples, lists, dicts, booleans, , none.
Comments
Post a Comment