Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将Json Dict对象转换为数据帧_Python_Json_Pandas_Dictionary - Fatal编程技术网

在Python中将Json Dict对象转换为数据帧

在Python中将Json Dict对象转换为数据帧,python,json,pandas,dictionary,Python,Json,Pandas,Dictionary,我从python中的web服务获得Json dict对象格式的一些嵌套输出。输出以嵌套Json dict对象的形式出现。现在,当我试图在python中将其转换为DataFrame时,parentkey不被视为列。我在一个键下有五个元素。我希望dataframe中总共显示6列 import pandas as pd data = {'2019-04-04 05:59:00': {'1. open': '1353.5500',

我从python中的web服务获得Json dict对象格式的一些嵌套输出。输出以嵌套Json dict对象的形式出现。现在,当我试图在python中将其转换为DataFrame时,parentkey不被视为列。我在一个键下有五个元素。我希望dataframe中总共显示6列

import pandas as pd
data = {'2019-04-04 05:59:00': 
                              {'1. open': '1353.5500', 
                              '2. high': '1354.8000', 
                              '3. low': '1353.0500', 
                              '4. close': '1353.0500', 
                              '5. volume': '25924'}, 
       '2019-04-04 05:58:00': {'1. open': '1354.2500', 
                               '2. high': '1354.2500', 
                               '3. low': '1353.4000', 
                               '4. close': '1353.4500', 
                               '5. volume': '38418'}
        }
df1=pd.DataFrame(data)
print(df1)

"""
  Output --
                2019-04-04 05:59:00 2019-04-04 05:58:00
  1. open             1353.5500           1354.2500
  2. high             1354.8000           1354.2500
  3. low              1353.0500           1353.4000
  4. close            1353.0500           1353.4500
  5. volume               25924               38418
"""

df2=df1.transpose()
print(df2)

""" 
  Output --
                         1. open    2. high     3. low   4. close 5. volume
2019-04-04 05:59:00  1353.5500  1354.8000  1353.0500  1353.0500     25924
2019-04-04 05:58:00  1354.2500  1354.2500  1353.4000  1353.4500     38418
"""
这里第一个日期字段被认为是索引,所以我的第一列从(1.open)开始,但我需要的是第一列应该是date

在此方面的帮助将不胜感激

结果应该是:

"""
Index    Date                 1. open    2. high    3. low     4. close      5. volume
0        2019-04-04 05:59:00  1353.5500  1354.8000  1353.0500  1353.0500     25924
1        2019-04-04 05:58:00  1354.2500  1354.2500  1353.4000  1353.4500     38418
"""
给你:

                  Date    1. open    2. high     3. low   4. close 5. volume
0  2019-04-04 05:58:00  1354.2500  1354.2500  1353.4000  1353.4500     38418
1  2019-04-04 05:59:00  1353.5500  1354.8000  1353.0500  1353.0500     25924
另外请注意,有一种更简单的方法可以使用
数据构建
df2

df2 = pd.DataFrame.from_dict(data, orient='index')
将这两部分放在一起:

pd.DataFrame.from_dict(data, orient='index').rename_axis(index='Date').reset_index()
要命名索引,可以在末尾添加
.rename_axis(index='index')

                      Date    1. open    2. high     3. low   4. close 5. volume
Index
0      2019-04-04 05:58:00  1354.2500  1354.2500  1353.4000  1353.4500     38418
1      2019-04-04 05:59:00  1353.5500  1354.8000  1353.0500  1353.0500     25924
                      Date    1. open    2. high     3. low   4. close 5. volume
Index
0      2019-04-04 05:58:00  1354.2500  1354.2500  1353.4000  1353.4500     38418
1      2019-04-04 05:59:00  1353.5500  1354.8000  1353.0500  1353.0500     25924