python - Pandas error with pivot_table and categorical data -


i'm having no fun on pandas. have data.frame:

 category1  category2   numeric 0   grey    complete    1 1   grey    complete    2 2   nan     incomplete  3 3   blue    complete    4 

and want pivot table.

p.pivot_table(df, index = 'category1', columns = 'category2', values = 'numeric', aggfunc = 'count') 

that's ok need add categories in category1, do:

df['category1_'] = p.categorical(df['category1'], categories = ['grey','blue','red']) 

and get:

p.pivot_table(df, index = 'category1_', columns = 'category2', values = 'numeric', aggfunc = 'count')  valueerror: cannot convert na integer 

i think there problem nan values in categorical series droping nan:

df2 = df.dropna()  p.pivot_table(df2, index = 'category1_', columns = 'category2', values = 'numeric', aggfunc = 'count')  c:\users\diego_000\anaconda3\lib\site-packages\pandas\core\common.py in _astype_nansafe(arr, dtype, copy)    2671     2672         if np.isnan(arr).any(): -> 2673             raise valueerror('cannot convert na integer')    2674     elif arr.dtype == np.object_ , np.issubdtype(dtype.type, np.integer):    2675         # work around numpy brokenness, #1987  valueerror: cannot convert na integer 

i dont know how solve this: categorical data pivot_table doesnt save categories, order , make error when there nan values.

am newbie pandas? or pandas categorical still in days?

thanks in advance.!


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -