pandasデータのイテレーション(forループ処理)
In [1]:
In [2]:
In [3]:
# DataFrame の列名と 列の値 ( Series ) からなる tuple をイテレーション
for column_name, item in df.iteritems():
print(column_name, type(column_name))
print(item, type(item))
print(item['Alice'], item[0], item.Alice)
print('======\n')
In [4]:
# DataFrame の行名と 行の値 ( Series ) からなる tuple をイテレーション
for index, row in df.iterrows():
print(index, type(index))
print(row, type(row))
print(row['point'], row[2], row.point)
print('======\n')
In [5]:
# DataFrame の**行名と 行の値からなるタプル ** をイテレーション
# デフォルトではPandasという名前のnamedtupleを返す。
for row in df.itertuples():
print(row, type(row))
print(row[3], row.point)
print('======')
In [6]:
# 引数nameをNoneとするとノーマルのタプルを返す。
for row in df.itertuples(name=None):
print(row, type(row))
print(row[3])
print('======\n')
In [7]:
In [8]:
for index, row in df.iterrows():
row.point /= 2
print(df)
In [9]:
for index, row in df.iterrows():
df.at[index, 'point'] /= 2
print(df)