2016-11-21 4 views
0

私はpd.DataFrameのためにpd.read_table(path/to/file, index_col=[0,1])に相当するものをお探ししています。私はPythonにこれらをロードした場合、私は上記のが、どのように私はprexisting pd.DataFrameを変換することができますように、私はちょうどindex_col=[0,1]を行うだろうPandasとPython 3を使用して、既存のpd.DataFrameオブジェクトのpd.MultiIndexを作成する方法は?

# Index Data 
iters = 3*[1] + 3*[2] + 3*[3] 
clusters = 3*[1,2,3] 

# Recreate DataFrame 
DF_A = pd.DataFrame([iters, clusters], index = ["iteration", "cluster"]).T 
DF_B = pd.DataFrame(np.random.RandomState(0).normal(size=(100,9)), index = ["attr_%d"%_ for _ in range(100)]).T 
DF_concat = pd.concat([DF_A, DF_B], axis=1).set_index("iteration", drop=True) 
DF_concat.head() 

enter image description here

私は頻繁に次の形式を持っているpd.DataFramesに遭遇しますpd.Indexにはpd.MultiIndexですので、iterationは外側のインデックスレベルで、clusterは内側のインデックスレベルですか?

私は以下のことを試しましたが、割り当てが乱れました。唯一私が作った簡単な例のための反復ごとに3があるはずです。これについて

DF_B.index = pd.MultiIndex(levels=[DF_concat["cluster"].index.tolist(), DF_concat["cluster"].tolist()], labels=[DF_concat["cluster"].index.tolist(), DF_concat["cluster"].tolist()], names=["iteration", "cluster"]) 
DF_B 

enter image description here

答えて

1

方法を...

DF_concat.set_index([DF_concat.index, 'cluster']) 
+0

私はあなたしながら、インデックスと呼ばれる可能性が知りませんでした」それを再設定する。ありがとう! –

関連する問題