问题
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