2017-12-27 404 views
0

我有這樣的JSON數據:轉換JSON到大熊貓數據幀

{ 
    "current": [ 
    [ 
     0, 
     "2017-01-15T00:08:36Z" 
    ], 
    [ 
     0, 
     "2017-01-15T00:18:36Z" 
    ] 
    ], 
    "voltage": [ 
    [ 
     12.891309987, 
     "2017-01-15T00:08:36Z" 
    ], 
    [ 
     12.8952162966, 
     "2017-01-15T00:18:36Z" 
    ] 
    ] 
} 

,我試圖進入它進入這個格式(時間序列)一個大熊貓數據幀:

time     current  voltage 
2017-01-15T00:08:36Z 0   12.891309987 
2017-01-15T00:18:36Z 0   12.8952162966 

我有嘗試:

t = pd.read_json(q) 

,但是這給了我:

     current        voltage 
0 [0, 2017-01-15T00:08:36Z] [12.891309987, 2017-01-15T00:08:36Z] 
1 [0, 2017-01-15T00:18:36Z] [12.8952162966, 2017-01-15T00:18:36Z] 

我怎樣才能把它變成正確的格式?

回答

2

如果兩列時間是一樣的,aftering閱讀JSON,我們可以選擇的值和Concat的他們:

ndf = pd.read_json(q) 

ndf = pd.concat([ndf.apply(lambda x : x.str[0]),ndf['current'].str[1].rename('time')],1) 

    current voltage     time 
0  0 12.891310 2017-01-15T00:08:36Z 
1  0 12.895216 2017-01-15T00:18:36Z 
+0

謝謝,這個工程。但是如何將「時間」列設置爲索引,如原始帖子中那樣?我試過:'ndf.set_index('time')。reset_index()',但這似乎不起作用。 – wazzahenry

+0

你爲什麼重置?只要'ndf.set_index('time')'就夠了 – Dark

1

據我所知,還沒有在read_json選項()來做到這一點。我的建議是在您讀取數據後重新操作表格。

t = pd.read_json('data.json') 
t['time'] = [x[1] for x in t['current']] 
t['current'] = [x[0] for x in t['current']] 
t['voltage'] = [x[0] for x in t['voltage']]