大熊猫-如何将数据从嵌套字典加载到数据框?

时间:2019-04-14 19:46:02

标签: python json python-3.x loops dictionary

我正在尝试创建一个具有股票收盘价的数据框,并找到了一个免费的api,该api以嵌套字典的形式返回与数据相关的JSON数据,如下所示:

{'name': 'AAPL',
 'history': 
           {'2019-01-04': 
                  {'open': '144.53',
                   'close': '148.26',
                   'high': '148.55',
                   'low': '143.80',
                   'volume': '58607070'},
           '2019-01-03': 
                   {'open': '143.98',
                    'close': '142.19',
                    'high': '145.72',
                    'low': '142.00',
                    'volume': '91312195'},
          '2019-01-02': 
                  {'open': '154.89',
                   'close': '157.92',
                   'high': '158.85',
                   'low': '154.23',
                   'volume': '37039737'
}}}

由于我想要的键“关闭”嵌套在“历史记录”中,并且每个特定的日期我都很难提取并将其放入数据框中。

在这种情况下如何前进/逻辑化?我试过用datetime生成日期列表,但没有成功。您有什么建议或建议吗?

编辑:当前代码,不能正常工作

def make_request():
    '''Makes a request to the API that returns a JSON-response '''
    r = requests.get(url)
    sample = json.loads(r.text)
    return sample


def check_keys(data):
    '''Checks the keys in the JSON-response'''
    print(data.keys())


def check_values(data):
    '''Checks the values in the JSON-respose'''
    print(data.values())


def get_values(data):
    '''Gets the date for each day in the sample and stores it in a list'''
     for v in data.get('history'):
        values = v
        return v


def get_closeprice(data, values):
    '''Uses the dates from get_values() to iterate through the sample and get the
    closing price for each date in the sample'''
    for date in values:
        data.get('history').get(values).get('close')
        return value

4 个答案:

答案 0 :(得分:2)

如果您只想将其加载到数据框中:

# h = your dictionary

df = pd.DataFrame.from_dict(data=h['history'],orient='index')

cols = ['close']

df = df[cols]

# Just as an aside Quandl has been very good for free financial data to me. 
#It has a paid side with premium data but I havent used it.

答案 1 :(得分:1)

您不需要知道存在哪个密钥即可访问它。您可以仅遍历字典中的所有键。

d = <your dict>
retval = {}

for k,v in d['history'].items():
    retval[k] = v['close']

print(retval)

答案 2 :(得分:1)

如果您知道自己的键,但是它们没有变化,我会使用Droids答案。 如果密钥可能会更改,那么这里是另一种解决方案。

d = {'name': 'AAPL',
 'history': 
           {'2019-01-04': 
                  {'open': '144.53',
                   'close': '148.26',
                   'high': '148.55',
                   'low': '143.80',
                   'volume': '58607070'},
           '2019-01-03': 
                   {'open': '143.98',
                    'close': '142.19',
                    'high': '145.72',
                    'low': '142.00',
                    'volume': '91312195'},
          '2019-01-02': 
                  {'open': '154.89',
                   'close': '157.92',
                   'high': '158.85',
                   'low': '154.23',
                   'volume': '37039737'
}}}
def print_nested_dict(nested_dict, name, prior_keys=[]):
    for key, value in nested_dict.items():
        # current_key_path is a list of each key we used to get here
        current_key_path = prior_keys + [key]
        # Convert that key path to a string
        key_path_str = ''.join('[\'{}\']'.format(key) for key in current_key_path)

        # If the value is a dict then recurse
        if isinstance(value, dict):
            print_nested_dict(value, name, current_key_path)
        else:
            # Else lets print the key and value for this value
            # along with where it was found
            print(key, value, '{}{}'.format(name, key_path_str))
print_nested_dict(d, "d")

输出:

name AAPL d['name']
open 144.53 d['history']['2019-01-04']['open']
close 148.26 d['history']['2019-01-04']['close']
high 148.55 d['history']['2019-01-04']['high']
low 143.80 d['history']['2019-01-04']['low']
volume 58607070 d['history']['2019-01-04']['volume']
open 143.98 d['history']['2019-01-03']['open']
close 142.19 d['history']['2019-01-03']['close']
high 145.72 d['history']['2019-01-03']['high']
low 142.00 d['history']['2019-01-03']['low']
volume 91312195 d['history']['2019-01-03']['volume']
open 154.89 d['history']['2019-01-02']['open']
close 157.92 d['history']['2019-01-02']['close']
high 158.85 d['history']['2019-01-02']['high']
low 154.23 d['history']['2019-01-02']['low']
volume 37039737 d['history']['2019-01-02']['volume']

话虽这么说,使用内置的dataframe方法可能会比这更有效。

答案 3 :(得分:1)

您可以使用正则表达式:

import re

if re.match(r"^(\d+-\d+-\d+)$", key):
    # do something with it's values.

但是,您将需要自己遍历字典。