简体   繁体   English

类型错误:int64 类型的 Object 不是 JSON 可序列化 | pandas 聚合函数和 json.dumps() 错误

[英]TypeError: Object of type int64 is not JSON serializable | pandas aggregation functions and json.dumps() error

Pandas aggregation functions return TypeError: Object of type int64 is not JSON serializable. Pandas 聚合函数返回类型错误:Object 类型 int64 不是 JSON 可序列化。

Here is the dataframe:这是 dataframe:

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df
Out[47]: 
   col1  col2
0     1     3
1     2     4

Here is how I am aggregating the column:这是我聚合该列的方式:

sum_col = df.col1.sum()
sum_col
Out[49]: 3

But as soon as I do json.dumps() it gives a type error:但是一旦我执行 json.dumps() 它就会出现类型错误:

data = json.dumps(sum_col)
data
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-5d4b966e64cc> in <module>
----> 1 data = json.dumps(sum_col)
      2 data
TypeError: Object of type int64 is not JSON serializable

I resolved this one.我解决了这个。

Pandas aggregation functions (like sum, count and mean) returns a NumPy int64 type number, not a Python integer. Although it looks exactly like a python integer. Pandas 聚合函数(如 sum、count 和 mean)返回 NumPy int64 类型的数字,而不是 Python integer。虽然它看起来完全像 python integer。

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
sum_col = df.col1.sum()
type(sum_col)

Out[52]: numpy.int64

This can be fixed by using python's int() function.这可以通过使用 python 的 int() function 来修复。

sum_col = int(df.col1.sum())
data = json.dumps(sum_col)
data

Out[56]: '3'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:int64 类型的对象不是 JSON 可序列化的 - TypeError: Object of type int64 is not JSON serializable Python - 类型错误:“int64”类型的 Object 不是 JSON 可序列化的 - Python - TypeError: Object of type 'int64' is not JSON serializable Python 错误:TypeErrpr:int64 类型的对象不是 JSON 可序列化的 - Python error: TypeErrpr: Object of type int64 is not JSON serializable Python-插入数据时发生错误(TypeError:“ int64”类型的对象不可JSON序列化) - Python - Error when inserting data (TypeError: Object of type 'int64' is not JSON serializable) 嵌套的JSON值导致“ TypeError:类型&#39;int64&#39;的对象不可JSON序列化” - Nested JSON Values cause “TypeError: Object of type 'int64' is not JSON serializable” Folium 图返回“TypeError:int64 类型的对象不是 JSON 可序列化的” - Folium plot returns 'TypeError: Object of type int64 is not JSON serializable' 导出到 Pyvis 的 Networkx 图 - 类型错误:int64 类型的 Object 不是 JSON 可序列化 - Networkx graph exporting to Pyvis - TypeError: Object of type int64 is not JSON serializable Python json.dumps TypeError: Object of type 'set' is not JSON serializable, when trying convert from variable, working when hardcoded - Python json.dumps TypeError: Object of type 'set' is not JSON serializable , when trying convert from variable , working when hardcoded Dict类型在json.dumps中引发错误 - Dict type throws error in json.dumps Python ::熊猫和json.dumps - Python :: pandas and json.dumps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM