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

时间:2017-08-23 00:29:48

标签: python pandas dictionary dataframe unix-timestamp

我有一个字典,我从一个有6个键的json url中提取。我的兴趣只在于密钥'value'的值。数据的结构如下:

    [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

3 个答案:

答案 0 :(得分:3)

假设您将'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

答案 1 :(得分:2)

如果我理解正确,pandas应该能够将其转换为数据帧:

df = pd.DataFrame(values_dictionary).rename(columns={'x':'Date'})

然后您可以使用to_datetime将其转换为yyyy / mm / dd格式:

df['Date'] = pd.to_datetime(df['Date'].astype(str), unit='s')

输出:

    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

答案 2 :(得分:0)

我不确定为什么你想要一本字典,如果你想要的只是日期。您可以执行此操作,只需获取日期列表。

import datetime
dates = [datetime.datetime.fromtimestamp(xydict['x']).strftime("%Y-%m-%d") for xydict in values]

编辑:如果你想要一个类似的字典格式:

import datetime 
dates = [{'date' : datetime.datetime.fromtimestamp(xydict['x']).strftime("%Y-%m-%d")} for xydict in values]