Python-pandas-iteration
Pythonパンダ-反復
Pandasオブジェクトに対する基本的な反復の動作は、タイプによって異なります。 Seriesを反復処理する場合、それは配列のようなものと見なされ、基本的な反復処理によって値が生成されます。 DataFrameやPanelなどの他のデータ構造は、オブジェクトの keys を反復処理する* dictのような*規則に従います。
要するに、基本的な反復(オブジェクトの i の場合)は次を生成します-
- シリーズ-値
- DataFrame -列ラベル
- パネル-アイテムのラベル
DataFrameの反復
DataFrameを反復すると、列名が与えられます。 同じことを理解するために、次の例を考えてみましょう。
import pandas as pd
import numpy as np
N=20
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
for col in df:
print col
その*出力*は次のとおりです-
A
C
D
x
y
DataFrameの行を反復するには、次の関数を使用できます-
- * iteritems()*-(キー、値)のペアを反復処理する
- * iterrows()*-(インデックス、シリーズ)のペアとして行を反復処理する
- * itertuples()*-namedtuplesとして行を反復処理します
iteritems()
各列をキーとして、値のペアをラベルとしてキー、列の値をシリーズオブジェクトとして繰り返します。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3'])
for key,value in df.iteritems():
print key,value
その*出力*は次のとおりです-
col1 0 0.802390
1 0.324060
2 0.256811
3 0.839186
Name: col1, dtype: float64
col2 0 1.624313
1 -1.033582
2 1.796663
3 1.856277
Name: col2, dtype: float64
col3 0 -0.022142
1 -0.230820
2 1.160691
3 -0.830279
Name: col3, dtype: float64
各列は、シリーズのキーと値のペアとして個別に繰り返されます。
iterrows()
iterrows()は、各インデックス値と各行のデータを含むシリーズを生成するイテレータを返します。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row_index,row in df.iterrows():
print row_index,row
その*出力*は次のとおりです-
0 col1 1.529759
col2 0.762811
col3 -0.634691
Name: 0, dtype: float64
1 col1 -0.944087
col2 1.420919
col3 -0.507895
Name: 1, dtype: float64
2 col1 -0.077287
col2 -0.858556
col3 -0.663385
Name: 2, dtype: float64
3 col1 -1.638578
col2 0.059866
col3 0.493482
Name: 3, dtype: float64
注-* iterrows()*は行を反復処理するため、行全体でデータ型を保持しません。 0,1,2は行インデックスであり、col1、col2、col3は列インデックスです。
itertuples()
itertuples()メソッドは、DataFrameの各行に対して名前付きタプルを生成する反復子を返します。 タプルの最初の要素は行の対応するインデックス値であり、残りの値は行の値です。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row in df.itertuples():
print row
その*出力*は次のとおりです-
Pandas(Index=0, col1=1.5297586201375899, col2=0.76281127433814944, col3=-
0.6346908238310438)
Pandas(Index=1, col1=-0.94408735763808649, col2=1.4209186418359423, col3=-
0.50789517967096232)
Pandas(Index=2, col1=-0.07728664756791935, col2=-0.85855574139699076, col3=-
0.6633852507207626)
Pandas(Index=3, col1=0.65734942534106289, col2=-0.95057710432604969,
col3=0.80344487462316527)
注意-反復中はオブジェクトを変更しないでください。 反復は読み取りを目的としており、反復子は元のオブジェクト(ビュー)のコピーを返すため、変更は元のオブジェクトに反映されません。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for index, row in df.iterrows():
row['a'] = 10
print df
その*出力*は次のとおりです-
col1 col2 col3
0 -1.739815 0.735595 -0.295589
1 0.635485 0.106803 1.527922
2 -0.939064 0.547095 0.038585
3 -1.016509 -0.116580 -0.523158
観察、変更は反映されません。