2017-09-11 10 views
1

名前を使用してインデックス列の特定の値にアクセスしながら、MultiIndexでDataFrameを繰り返し処理したいとします。例えば、私はのようなコードを使用してtableの行を反復処理したいインデックス名でMultiIndexでpandas.DataFrameを繰り返し処理します

import pandas as pd 
index = pd.MultiIndex.from_product([range(2), range(3)], names=['index_a', 'index_b']) 
table = pd.DataFrame({'my_column': range(len(index))}, index=index) 

次与えられる:

for row in named_index_iterator(table): 
    print(row.my_column, row.index_a, row.index_b) 

または

for row in named_index_iterator(table): 
    print(row.my_column, row.Index.index_a, row.Index.index_b) 

私はitertuplesを使用することはできません named_index_iteratorを実装しますか、 DataFrameのように、インデックス用の名前付きタプルではないプレーンタプルを返します。 table.indexの反復子が再び平野タプルではなく、名前のタプルを与えるよう

for data_row, index_row: itertools.zip_longest(table.itertuples(), table.index): 

:同様に、私のようなものを使用することはできません。私は

for row in table.reset_index().itertuples(): 

が、コピーテーブルというを使用する今の回避策として

+0

私はあなたが何を求めているのか理解しています。私は現在、パンダAPIがMultiIndicesの明示的な名前付きタプルをサポートしているとは考えていません。あなたの唯一の選択肢は、インデックスをリセットすることです。あなたのデータフレームは非常に大きいですか? –

+0

df.itertuples()の 'for行に何が問題なのですか:\ n index_a、index_b = row.Index \ n ...'? 'index = {k、vのk、vのzip(df.index.names、row.Index})' –

+0

@COLDSPEED - コピーを避けるためにデータがそれほど大きくないので、私はMultiIndex API –

答えて

0

参照のための独自の質問に答える。行のタプルから索引名を削除するために改善することができる

import collections 
import pandas as pd 
index = pd.MultiIndex.from_product([range(2), range(3)], names=['index_a', 'index_b']) 
table = pd.DataFrame({'my_column': range(len(index))}, index=index) 
print(table) 

def df_iter_with_index_names(table): 
    IndexNames = collections.namedtuple('IndexNames', table.index.names) 
    for row in table.itertuples(): 
     yield (IndexNames(*row.Index), row) 

for index, row in df_iter_with_index_names(table): 
    print(index.index_a, row.my_column) 

、:のような使用に

import collections 

def df_iter_with_index_names(table): 
    IndexNames = collections.namedtuple('IndexNames', table.index.names) 
    for row in table.itertuples(): 
     yield (IndexNames(*row.Index), row) 

は私が名前によってインデックスを反復するために、次のユーティリティを作成しました残り物は DataFrame.itertuples()ですが、私はそれで生きることができます。

関連する問題