2017-05-04 67 views
1

我的數據幀l爲這樣的:有毛病dataframe.to_json()

      0 1 
Sepal Length Label  
(4.296, 5.2] setosa  39 0.866667 
       versicolor  5 0.111111 
       virginica  1 0.022222 
(5.2, 6.1] setosa  11 0.220000 
       versicolor 29 0.580000 
       virginica  10 0.200000 
(6.1, 7]  versicolor 16 0.372093 
       virginica  27 0.627907 
(7, 7.9]  virginica  12 1.000000 

然後EXCUTE LL = l.to_json(),結果是:

'{"0":{"["(4.296, 5.2]","setosa"]":39,"["(4.296, 5.2]","versicolor"]":5,"["(4.296, 5.2]","virginica"]":1,"["(5.2, 6.1]","setosa"]":11,"["(5.2, 6.1]","versicolor"]":29,"["(5.2, 6.1]","virginica"]":10,"["(6.1, 7]","versicolor"]":16,"["(6.1, 7]","virginica"]":27,"["(7, 7.9]","virginica"]":12},"1":{"["(4.296, 5.2]","setosa"]":0.8666666667,"["(4.296, 5.2]","versicolor"]":0.1111111111,"["(4.296, 5.2]","virginica"]":0.0222222222,"["(5.2, 6.1]","setosa"]":0.22,"["(5.2, 6.1]","versicolor"]":0.58,"["(5.2, 6.1]","virginica"]":0.2,"["(6.1, 7]","versicolor"]":0.3720930233,"["(6.1, 7]","virginica"]":0.6279069767,"["(7, 7.9]","virginica"]":1.0}}' 

然後我嘗試閱讀ll,做pd.read_json(ll)並失敗。以下是消息:

ValueError        Traceback (most recent call last) 
<ipython-input-574-4cb1a3ab2e3c> in <module>() 
----> 1 pd.read_json(l1g) 

/home/lv/anaconda3/lib/python3.6/site-packages/pandas/io/json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines) 
    279   obj = FrameParser(json, orient, dtype, convert_axes, convert_dates, 
    280       keep_default_dates, numpy, precise_float, 
--> 281       date_unit).parse() 
    282 
    283  if typ == 'series' or obj is None: 

/home/lv/anaconda3/lib/python3.6/site-packages/pandas/io/json.py in parse(self) 
    347 
    348   else: 
--> 349    self._parse_no_numpy() 
    350 
    351   if self.obj is None: 

/home/lv/anaconda3/lib/python3.6/site-packages/pandas/io/json.py in _parse_no_numpy(self) 
    564   if orient == "columns": 
    565    self.obj = DataFrame(
--> 566     loads(json, precise_float=self.precise_float), dtype=None) 
    567   elif orient == "split": 
    568    decoded = dict((str(k), v) 

ValueError: No ':' found when decoding object value 

我想保存多指標的數據的結構,所以怎麼辦

誰能幫助我感謝>。 <

回答

0

大熊貓JSON出口和往返仍然有多指標困難(根據this github issue)來解決這個是做出口前reset_index和​​之後,在GitHub的問題提到

一種方式, this previous answer

你有沒有嘗試其他orient的(pandas documentation

+0

好吧,我kown其他定向的一個,但結果不是我想要的。 – labixiaoK

+0

之後做一個reset_index似乎是我想要的。順便說一句,我主要運輸JSON到d3.js,希望它能與reset_index表單一起使用。無論如何,非常感謝。 – labixiaoK