[英]Pandas loads JSON file DatetimeIndex and not float
I have the following JSON file which I saved from a pandas dataframe: 我从熊猫数据框中保存了以下JSON文件:
{
"A": {
"69.0": 0,
"69.5": 0,
"70.0": 0,
"70.5": 0,
"71.0": 0
},
"B": {
"69.0": 1,
"69.5": 2,
"70.0": 3,
"70.5": 4,
"71.0": 5
},
"C": {
"69.0": 1,
"69.5": 1,
"70.0": 1,
"70.5": 1,
"71.0": 1
}
}
When I run 当我跑步
df = pd.read_json("df.json")
I get the dataframe but the index is a DatetimeIndex and df.tail()
returns: 我得到了数据df.tail()
但是索引是DatetimeIndex,并且df.tail()
返回:
A B C
1970-01-01 00:01:09.000 0 1 1
1970-01-01 00:01:09.500 0 2 1
1970-01-01 00:01:10.000 0 3 1
1970-01-01 00:01:10.500 0 4 1
1970-01-01 00:01:11.000 0 5 1
I want the index to be a float and not a DatetimeIndex
. 我希望索引是一个float而不是DatetimeIndex
。 How can I load the JSON with the correct index type? 如何加载具有正确索引类型的JSON? I cannot change the original JSON. 我无法更改原始JSON。
Thanks 谢谢
Assuming you have the following dictionary : 假设您有以下字典 :
In [101]: d
Out[101]:
{'A': {'69.0': 0, '69.5': 0, '70.0': 0, '70.5': 0, '71.0': 0},
'B': {'69.0': 1, '69.5': 2, '70.0': 3, '70.5': 4, '71.0': 5},
'C': {'69.0': 1, '69.5': 1, '70.0': 1, '70.5': 1, '71.0': 1}}
you can construct a DF as follows: 您可以按以下方式构造DF:
In [102]: pd.DataFrame(d)
Out[102]:
A B C
69.0 0 1 1
69.5 0 2 1
70.0 0 3 1
70.5 0 4 1
71.0 0 5 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.