Saturday, 22 June 2019

Using pandas pivot_table with Interval columns results in TypeError

      cat1             cat2                       col_a             col_b
0    (34.0, 38.0]    (15.9, 47.0]             29               10
1    (34.0, 38.0]    (15.9, 47.0]             37               22
2    (28.0, 34.0]    (47.0, 56.0]              3               13
3    (34.0, 38.0]    (47.0, 56.0]             15                7
4    (28.0, 34.0]    (56.0, 67.0]             42               20
5    (28.0, 34.0]    (47.0, 56.0]             31               23
6    (28.0, 34.0]    (56.0, 67.0]             26               17
7    (28.0, 34.0]    (56.0, 67.0]              7                1
8    (28.0, 34.0]    (56.0, 67.0]             36               19
9    (19.0, 28.0]    (56.0, 67.0]              5                7
10   (19.0, 28.0]    (56.0, 67.0]             21                5
11   (28.0, 34.0]    (67.0, 84.0]             37               13

In the dataframe above, I want to do this pivot table operation using pandas

pd.pivot_table(df, index='cat1', columns='cat2', values='col_a')

but I get the error:

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

Both col_a and col_b are of type int32, and cat1 and cat2 are of type categorical. How do I get rid of this error?



from Using pandas pivot_table with Interval columns results in TypeError

No comments:

Post a Comment