Reputation: 881
I have a dictionary that I have extracted from a json url that has 6 keys. My interest is only in the values for the key 'value'
. The data is structured like this:
[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}]
Where 'x'
is the unix timestamp and 'y'
is the value for that time.
How can I extract the values from the 'value'
dictionary and restructure them so that 'x'
is labelled 'date'
and structured in this format: 2011-09-13
?
Upvotes: 3
Views: 2306
Reputation: 579
if i understood you correctly, pandas should be able to convert it to a dataframe:
df = pd.DataFrame(values_dictionary).rename(columns={'x':'Date'})
then you can use the to_datetime to convert it to yyyy/mm/dd format:
df['Date'] = pd.to_datetime(df['Date'].astype(str), unit='s')
output:
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
Upvotes: 2
Reputation: 637
I'm not sure why you would want a dictionary if all you want is the dates. You can do this and just get a list of the dates.
import datetime
dates = [datetime.datetime.fromtimestamp(xydict['x']).strftime("%Y-%m-%d") for xydict in values]
EDIT: if you want this in a similar dictionary format:
import datetime
dates = [{'date' : datetime.datetime.fromtimestamp(xydict['x']).strftime("%Y-%m-%d")} for xydict in values]
Upvotes: 0
Reputation: 152587
Assuming you assign what is kept in 'values'
to a variable called lst
(e.g. lst = data['value']
) you can use this:
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]})
With:
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}]
This gives me this 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
Upvotes: 3