AST module ['zero' 'zero' 'zero' 'zero' 2 'zero' 'zero' 'zero' 'zero' 'zero' 'zero' 7 ^ SyntaxError: invalid syntax

大憨熊 提交于 2019-12-24 20:09:30

问题


I have a pandas data frame, It contains a column that looks like this

0    [zero, zero, zero, zero, 2, zero, zero, zero, ...
1    [zero, zero, zero, 3, zero, zero, zero, 4, zer...
2    [zero, zero, zero, zero, zero, zero, zero, 2, ...
3    [1, zero, 6, 1, zero, zero, zero, zero, zero, ...
4    [zero, zero, zero, zero, 6, zero, zero, [2, 0]...
Name: y, dtype: object

this column is the output of this function

def santa(the_frame):

    g = dict([[key, [*g]] for key, g in groupby(the_frame, key=lambda x: (x-1)//7)])

    #d = [((list([c%7 for c in g[i]]) if len(g[i]) > 1 else g[i][0]%7) if (i in g) else 'zero') for i in range(0, 143))]    

    d = np.array([((np.array([c%7 for c in g[i]], dtype=object) if len(g[i]) > 1 else g[i][0]%7) if (i in g) else 'zero') for i in range(0, 143)])    


    return(d)

I was trying to remove the 0 in the last line inside the list[2,0]

I tried this

x['y'] = x['y'].astype(str).str.replace('0', '7').apply(ast.literal_eval)

which was working fine but then I tried replacing the list expression in my function with a Numpy array as in the final line of the function and now it gives me this error when I run the line above

['zero' 'zero' 'zero' 'zero' 2 'zero' 'zero' 'zero' 'zero' 'zero' 'zero' 7            ^ SyntaxError: invalid syntax

what is the problem here and how do I fix it?

Thanks in advance.

来源:https://stackoverflow.com/questions/46879936/ast-module-zero-zero-zero-zero-2-zero-zero-zero-zero-zero-ze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!