I am trying to retrieve val1 and val2 values from the following nested json file to build a pandas dataframe with two columns: val1 and val2:
{
'start': '2015-10-01 00:00',
'end': '2015-10-01 01:00',
'records':
{
'val1':
[
1,
2,
3,
4,
5
],
'val2':
[
0.1,
0.5,
0.2,
0.1,
0.0
],
'val3': 'abc'
}
}
This is what I do:
import json
from pandas.io.json import json_normalize
with open(json_file) as data_file:
data = json.load(data_file)
df = json_normalize(data, 'records', ['val1', 'val2'], record_prefix='records_', errors='ignore')
However, I get this output:
records_0 val1 val2
0 val1 NaN NaN
1 val2 NaN NaN
2 val3 NaN NaN
The expected output:
val1 val2
1 0.1
2 0.5
3 0.2
4 0.1
5 0.0

