因为numpy的int类型无法被json化,所以需要将numpy的int转为原生类型。
解决方案# pandas返回的
sex_cnt = marks['sex'].value_counts()
type(sex_cnt['男']) # numpy.int64
# 3种转化方法
# examples using a.item()
type(np.float32(0).item()) #
type(np.float64(0).item()) #
type(np.uint32(0).item()) #
# examples using np.asscalar(a)
type(np.asscalar(np.int16(0))) #
type(np.asscalar(np.cfloat(0))) #
type(np.asscalar(np.datetime64(0, 'D'))) #
type(np.asscalar(np.timedelta64(0, 'D'))) #
# 强制转化
int(np.int64(0))
References
python中的常见错误