简体   繁体   English

将Json加载到Pandas数据帧中

[英]Loading Json into Pandas dataframe

I have a valid json file with the following format that I am trying to load into pandas. 我有一个有效的json文件,其格式如下,我正在尝试加载到pandas中。

{
    "testvalues": [
        [1424754000000, 0.7413],
        [1424840400000, 0.7375],
        [1424926800000, 0.7344],
        [1425013200000, 0.7375],
        [1425272400000, 0.7422],
        [1425358800000, 0.7427]
    ]
}

There is a Pandas function called read_json() that takes in json files/buffers and spits out the dataframe but I have not been able to get it to load correctly, which is to show two columns rather than a single column with elements looking like [1424754000000, 0.7413]. 有一个名为read_json()的Pandas函数,它接收json文件/缓冲区并吐出数据帧,但是我无法正确加载它,这是显示两列而不是单列,其元素看起来像[ 1424754000000,0.7413]。 I have tried different 'orient' and 'typ' to no avail. 我试过不同的'东方'和'典型'无济于事。 What options should I pass into the function to get it to spit out a two column dataframe corresponding the timestamp and the value? 我应该将哪些选项传递给函数,以使其吐出与时间戳和值相对应的两列数据帧?

I'm not sure about pandas read_json but IIUC you could do that with astype(str) , str.split , str.strip : 我不确定pandas read_json但是IIUC你可以用astype(str)str.splitstr.strip做到这str.strip

d = {
    "testvalues": [
        [1424754000000, 0.7413],
        [1424840400000, 0.7375],
        [1424926800000, 0.7344],
        [1425013200000, 0.7375],
        [1425272400000, 0.7422],
        [1425358800000, 0.7427]
    ]
}

df = pd.DataFrame(d)
res = df.testvalues.astype(str).str.strip('[]').str.split(', ', expand=True)


In [112]: df
Out[112]:
                testvalues
0  [1424754000000, 0.7413]
1  [1424840400000, 0.7375]
2  [1424926800000, 0.7344]
3  [1425013200000, 0.7375]
4  [1425272400000, 0.7422]
5  [1425358800000, 0.7427]

In [113]: res
Out[113]:
               0       1
0  1424754000000  0.7413
1  1424840400000  0.7375
2  1424926800000  0.7344
3  1425013200000  0.7375
4  1425272400000  0.7422
5  1425358800000  0.7427

You can apply a function that splits it into a pd.Series . 您可以apply将其拆分为pd.Series

Say you start with 假设你开始

df = pd.read_json(s)

Then just apply a splitting function: 然后只应用拆分功能:

>>>     df.apply(
    lambda r: pd.Series({'l': r[0][0], 'r': r[0][1]}),
    axis=1)
    l   r
0   1.424754e+12    0.7413
1   1.424840e+12    0.7375
2   1.424927e+12    0.7344
3   1.425013e+12    0.7375
4   1.425272e+12    0.7422
5   1.425359e+12    0.7427

You can use list comprehension with DataFrame contructor: 您可以在DataFrame构造函数中使用list comprehension:

import pandas as pd

df = pd.read_json('file.json')
print df
                testvalues
0  [1424754000000, 0.7413]
1  [1424840400000, 0.7375]
2  [1424926800000, 0.7344]
3  [1425013200000, 0.7375]
4  [1425272400000, 0.7422]
5  [1425358800000, 0.7427]

print pd.DataFrame([x for x in df['testvalues']], columns=['a','b'])
               a       b
0  1424754000000  0.7413
1  1424840400000  0.7375
2  1424926800000  0.7344
3  1425013200000  0.7375
4  1425272400000  0.7422
5  1425358800000  0.7427

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM