我注意到了这种行为,不确定这是一个错误。 我创建了一个包含2个整数列和1个浮点列的数据框
import pandas as pd
df = pd.DataFrame([[1,2,0.2],[3,2,0.1]])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data columns (total 3 columns):
0 2 non-null int64
1 2 non-null int64
2 2 non-null float64
dtypes: float64(1), int64(2)
如果我将其输出到Json,则dtype信息将丢失:
df.to_json(orient= 'records')
'[{"0":1.0,"1":2.0,"2":0.2},{"0":3.0,"1":2.0,"2":0.1}]'
所有数据都转换为浮点数。如果例如一列包含ns时间戳,则会出现问题,因为它们会转换为指数表示法,并且亚秒信息会丢失。
我还在此处提交了问题:https://github.com/pydata/pandas/issues/7583
我期待的结果是:
'[{"0":1,"1":2,"2":0.2},{"0":3,"1":2,"2":0.1}]'
答案 0 :(得分:1)
一种方法是使用对象dtype查看DataFrame列:
In [11]: df1 = df.astype(object)
In [12]: df1.to_json()
Out[12]: '{"0":{"0":1,"1":3},"1":{"0":2,"1":2},"2":{"0":0.2,"1":0.1}}'
In [13]: df1.to_json(orient='records')
Out[13]: '[{"0":1,"1":2,"2":0.2},{"0":3,"1":2,"2":0.1}]'