2016-10-21 14 views
2

まず、私はpython 3.50をjupyterノートブックで使用しています。Python pandas Multindexカラム

レポートにデータを表示するためのDataFrameを作成したいとします。私はそれが2つのインデックスの列(それを参照する用語が正しい場合は私を許してください。私はパンダで動作するために使用していない)を持っています。

私が働く。このサンプルコードがあります。

frame = pd.DataFrame(np.arange(12).reshape((4, 3)), 
        index =[['a', 'a', 'b', 'b'], [1, 2, 1, 2]], 
        columns =[['Ohio', 'Ohio', 'Ohio'], ['Green', 'Red', 'Green']]) 

をしかし、私は、私の場合にそれを取るしようとすると、それは私にエラー与える:

cell_rise_Inv= pd.DataFrame([[0.00483211, 0.00511619, 0.00891821, 0.0449637, 0.205753], 
          [0.00520049, 0.00561577, 0.010993, 0.0468998, 0.207461], 
          [0.00357213, 0.00429087, 0.0132186, 0.0536389, 0.21384], 
          [-0.0021868, -0.0011312, 0.0120546, 0.0647213, 0.224749], 
          [-0.0725403, -0.0700884, -0.0382486, 0.0899121, 0.313639]], 
          index =[['transition [ns]','transition [ns]','transition [ns]','transition [ns]','transition [ns]'], 
            [0.0005, 0.001, 0.01, 0.1, 0.5]], 
          columns =[[0.01, 0.02, 0.05, 0.1, 0.5],['capacitance [pF]','capacitance [pF]','capacitance [pF]','capacitance [pF]','capacitance [pF]']]) 
cell_rise_Inv 

--------------------------------------------------------------------------- 
AssertionError       Traceback (most recent call last) 
<ipython-input-89-180a1ad88403> in <module>() 
     6        index =[['transition [ns]','transition [ns]','transition [ns]','transition [ns]','transition [ns]'], 
     7         [0.0005, 0.001, 0.01, 0.1, 0.5]], 
----> 8        columns =[[0.01, 0.02, 0.05, 0.1, 0.5],['capacitance [pF]','capacitance [pF]','capacitance [pF]','capacitance [pF]','capacitance [pF]']]) 
     9 cell_rise_Inv 

C:\Users\Josele\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy) 
    261      if com.is_named_tuple(data[0]) and columns is None: 
    262       columns = data[0]._fields 
--> 263      arrays, columns = _to_arrays(data, columns, dtype=dtype) 
    264      columns = _ensure_index(columns) 
    265 

C:\Users\Josele\Anaconda3\lib\site-packages\pandas\core\frame.py in _to_arrays(data, columns, coerce_float, dtype) 
    5350  if isinstance(data[0], (list, tuple)): 
    5351   return _list_to_arrays(data, columns, coerce_float=coerce_float, 
-> 5352        dtype=dtype) 
    5353  elif isinstance(data[0], collections.Mapping): 
    5354   return _list_of_dict_to_arrays(data, columns, 

C:\Users\Josele\Anaconda3\lib\site-packages\pandas\core\frame.py in _list_to_arrays(data, columns, coerce_float, dtype) 
    5429   content = list(lib.to_object_array(data).T) 
    5430  return _convert_object_array(content, columns, dtype=dtype, 
-> 5431         coerce_float=coerce_float) 
    5432 
    5433 

C:\Users\Josele\Anaconda3\lib\site-packages\pandas\core\frame.py in _convert_object_array(content, columns, coerce_float, dtype) 
    5487    # caller's responsibility to check for this... 
    5488    raise AssertionError('%d columns passed, passed data had %s ' 
-> 5489         'columns' % (len(columns), len(content))) 
    5490 
    5491  # provide soft conversion of object dtypes 

AssertionError: 2 columns passed, passed data had 5 columns 

任意のアイデア?私はこの例題がなぜ機能するのか理解できず、私はそれをしません。 :S

ありがとうございます。

+0

エラーは、インデックスに一致するシェイプのデータを渡していないことを示します。AssertionError:2カラムが渡され、5カラムが渡されました –

+0

インデックスが 'capacitance [pF]' 'データには2つのカラムしかありません... –

+0

また、ラベルの順番( '' capacitance [pF] '')とマルチインデックスの数字を切り替えることができます。 –

答えて

2

を使用すると思います。実際には、あなたのリストの周りnp.array(...)を追加するだけで正常に動作:

 
cell_rise_Inv= pd.DataFrame(
     np.array([[0.00483211, 0.00511619, 0.00891821, 0.0449637, 0.205753], 
        [0.00520049, 0.00561577, 0.010993, 0.0468998, 0.207461], 
        [0.00357213, 0.00429087, 0.0132186, 0.0536389, 0.21384], 
        [-0.0021868, -0.0011312, 0.0120546, 0.0647213, 0.224749], 
        [-0.0725403, -0.0700884, -0.0382486, 0.0899121, 0.313639]]), 
     index=[['transition [ns]'] * 5, 
       [0.0005, 0.001, 0.01, 0.1, 0.5]], 
     columns=[['capacitance [pF]'] * 5, 
       [0.01, 0.02, 0.05, 0.1, 0.5]]) 

私は、インデックス内の繰り返し文字列を短くし、インデックス・レベルの順序を入れ替えたが、それらは重要な変更はありません。

編集 少し調査しましたか?ネストされたリスト(np.arrayコールなし)を渡すと、columnsなしでコールが機能し、columnsが1Dリストであってもコールが機能します。何らかの理由で、入力がndarrayでない限り、2つの要素のネストされたリストはマルチインデックスとして解釈されません。

私はこの質問に基づいてパンダにissue #14467を提出しました。

2

これは矛盾しているようです。例では、入力ではなく、ネストされたリストとしてnumpy配列を渡します。私はあなたのコードと例の間に1つの大きな違いがありpd.MultiIndexコンストラクタfrom_arrays

idx = pd.MultiIndex.from_arrays([['transition [ns]'] * 5, 
           [0.0005, 0.001, 0.01, 0.1, 0.5]]) 
col = pd.MultiIndex.from_arrays([[0.01, 0.02, 0.05, 0.1, 0.5], 
           ['capacitance [pF]'] * 5]) 

cell_rise_Inv= pd.DataFrame([[0.00483211, 0.00511619, 0.00891821, 0.0449637, 0.205753], 
          [0.00520049, 0.00561577, 0.010993, 0.0468998, 0.207461], 
          [0.00357213, 0.00429087, 0.0132186, 0.0536389, 0.21384], 
          [-0.0021868, -0.0011312, 0.0120546, 0.0647213, 0.224749], 
          [-0.0725403, -0.0700884, -0.0382486, 0.0899121, 0.313639]], 
          index=idx, 
          columns=col) 
cell_rise_Inv 

enter image description here

+0

悪い。私はOPの出力を完全に読まなかった。 Downvoteが削除されました。ゆっくりと、あなたにそれを取り出そうとはしなかった。 –

+0

@MadPhysicistダウン投票にコメントを残し、間違いを訂正するための功績。私は最近、そして間違って私が疲れていたので誰かの投稿を閉じました...それは起こります。 – piRSquared

関連する問題