从字典中提取键值作为数据框
Extracting key,values from a dictionary as a dataframe
我有一本从 json url 中提取的字典,它有 6 个键。我只对键 '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
?
如果我理解正确,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
如果您只需要日期,我不确定您为什么需要字典。您可以执行此操作并获得日期列表。
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]
假设您将 '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
我有一本从 json url 中提取的字典,它有 6 个键。我只对键 '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
?
如果我理解正确,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
如果您只需要日期,我不确定您为什么需要字典。您可以执行此操作并获得日期列表。
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]
假设您将 '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