Python 3 – pandasデータフレームの反復-Pythonチュートリアル

Python

forステートメントを使用したDataFrameループ(反復)。 列ごとに、パンダのデータフレームをループすることができます。

関連コース: Pythonパンダを使用したデータ分析

パンダの下。 例としてDataFrameを使用します。

1
2
3
4
5
6
import pandas as pd

df = pd.DataFrame({'age': [20, 32], 'state': ['NY', 'CA'], 'point': [64, 92]},
index=['Alice', 'Bob'])

print(df)

これにより、次のデータフレームが出力されます。

1
2
3
       age state  point
Alice 20 NY 64
Bob 32 CA 92

列をループする

DataFrameを直接forループに固定すると、列名(列名)は次の順序で取得されます。

1
2
3
4
for column_name in df:
print(type(column_name))
print(column_name)
print('------n')

この出力:

1
2
3
4
5
6
7
8
9
10
11
<class 'str'>
age
------

<class 'str'>
state
------

<class 'str'>
point
------

データフレームを反復する

.iteritems()

iteritems()メソッドを使用して列名(列名)を使用すると、列データ(pandas。Series)タプル(列名、Series)を取得できます。

1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd

df = pd.DataFrame({'age': [20, 32], 'state': ['NY', 'CA'], 'point': [64, 92]},
index=['Alice', 'Bob'])

for column_name, item in df.iteritems():
print(type(column_name))
print(column_name)
print('~~~~~~')

print(type(item))
print(item)
print('------')

この出力:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<class 'str'>
age
~~~~~~
<class 'pandas.core.series.Series'>
Alice 20
Bob 32
Name: age, dtype: int64
------
<class 'str'>
state
~~~~~~
<class 'pandas.core.series.Series'>
Alice NY
Bob CA
Name: state, dtype: object
------
<class 'str'>
point
~~~~~~
<class 'pandas.core.series.Series'>
Alice 64
Bob 92
Name: point, dtype: int64
------

.iterrows()

iterrows()メソッドを使用してインデックス名(行名)を使用すると、データ(pandas。Series)タプル(index、Series)を取得できます。

1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd

df = pd.DataFrame({'age': [20, 32], 'state': ['NY', 'CA'], 'point': [64, 92]},
index=['Alice', 'Bob'])

for index, row in df.iterrows():
print(type(index))
print(index)
print('~~~~~~')

print(type(row))
print(row)
print('------')

これにより、次のようになります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<class 'str'>
Alice
~~~~~~
<class 'pandas.core.series.Series'>
age 20
state NY
point 64
Name: Alice, dtype: object
------
<class 'str'>
Bob
~~~~~~
<class 'pandas.core.series.Series'>
age 32
state CA
point 92
Name: Bob, dtype: object
------

.itertuples()

itertuples()メソッドを使用して、インデックス名(行名)の列とその行のデータを一度に1行ずつ取得できます。 タプルの最初の要素はインデックス名です。

デフォルトでは、Pandasという名前のnamedtupleが返されます。 Namedtupleを使用すると、に加えて各要素の値にアクセスできます。 []。

1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd

df = pd.DataFrame({'age': [20, 32], 'state': ['NY', 'CA'], 'point': [64, 92]},
index=['Alice', 'Bob'])

for row in df.itertuples():
print(type(row))
print(row)
print('------')

print(row[3])
print(row.point)
print('------n')

これにより、次のように出力されます。

1
2
3
4
5
6
7
8
9
10
11
12
13
<class 'pandas.core.frame.Pandas'>
Pandas(Index='Alice', age=20, state='NY', point=64)
------
64
64
------

<class 'pandas.core.frame.Pandas'>
Pandas(Index='Bob', age=32, state='CA', point=92)
------
92
92
------

列の値を取得する

特定の列の値を順番に取得することができます。

上記のiterows()、itertuples()メソッドは、各行のすべての列の要素を取得できますが、特定の列の要素のみが必要な場合は、次のように記述することもできます。

1
2
3
4
5
6
7
print(df['age'])




print(type(df['age']))

Seriesをforループに適用すると、その値を順番に取得できます。 DataFrameで列を指定し、それをforループに適用すると、その列の値を順番に取得できます。

1
2
for age in df['age']:
print(age)

組み込み関数zip()を使用して、複数の列の値をまとめて取得することもできます。

1
2
for age, point in zip(df['age'], df['point']):
print(age, point)

インデックス(行名)を取得する場合は、index属性を使用します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
print(df.index)


print(type(df.index))


for index in df.index:
print(index)



for index, state in zip(df.index, df['state']):
print(index, state)


関連コース: Pythonパンダを使用したデータ分析

Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です