编程语言
首页 > 编程语言> > python – 从字典中提取键值作为数据帧

python – 从字典中提取键值作为数据帧

作者:互联网

我有一个字典,我从一个有6个键的json url中提取.我的兴趣只在于关键’价值’的价值.数据结构如下:

    [in] print(data)
    [out] ...'values': [{'x': 1230940800, 'y': 0}, 
{'x': 1231113600, 'y': 0}, 
{'x': 1231286400, 'y': 0}, 
{'x': 1231459200, 'y': 0}, 
{'x': 1231632000, 'y': 0}, 
{'x': 1231804800, 'y': 0}, 
{'x': 1231977600, 'y': 0}, 
{'x': 1232150400, 'y': 0}, 
{'x': 1232323200, 'y': 0}, 
{'x': 1232496000, 'y': 0}, 
{'x': 1232668800, 'y': 0}, 
{'x': 1232841600, 'y': 0}, 
{'x': 1233014400, 'y': 0}, 
{'x': 1233187200, 'y': 0}, 
{'x': 1233360000, 'y': 0}] 

其中’x’是unix时间戳,’y’是该时间的值.
如何从’value’字典中提取值并对其进行重组,以便’x’标记为’date’并以此格式构建:2011-09-13?

解决方法:

假设您将’values’中保存的内容分配给名为lst的变量(例如lst = data [‘value’]),您可以使用:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Date': np.array([subdct['x'] for subdct in lst], dtype='datetime64[s]'),
                   'y': [subdct['y'] for subdct in lst]})

附:

lst = [{'x': 1230940800, 'y': 0}, 
       {'x': 1231113600, 'y': 0}, 
       {'x': 1231286400, 'y': 0}, 
       {'x': 1231459200, 'y': 0}, 
       {'x': 1231632000, 'y': 0}, 
       {'x': 1231804800, 'y': 0}, 
       {'x': 1231977600, 'y': 0}, 
       {'x': 1232150400, 'y': 0}, 
       {'x': 1232323200, 'y': 0}, 
       {'x': 1232496000, 'y': 0}, 
       {'x': 1232668800, 'y': 0}, 
       {'x': 1232841600, 'y': 0}, 
       {'x': 1233014400, 'y': 0}, 
       {'x': 1233187200, 'y': 0}, 
       {'x': 1233360000, 'y': 0}]

这给了我这个df:

         Date  y
0  2009-01-03  0
1  2009-01-05  0
2  2009-01-07  0
3  2009-01-09  0
4  2009-01-11  0
5  2009-01-13  0
6  2009-01-15  0
7  2009-01-17  0
8  2009-01-19  0
9  2009-01-21  0
10 2009-01-23  0
11 2009-01-25  0
12 2009-01-27  0
13 2009-01-29  0
14 2009-01-31  0

标签:python,pandas,dataframe,dictionary,unix-timestamp
来源: https://codeday.me/bug/20190828/1750734.html