zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

Lesson9——Pandas iteration遍历

2023-02-18 16:33:24 时间

pandas目录

1 简介

  对于 Series 而言,您可以把它当做一维数组进行遍历操作;而像 DataFrame 这种二维数据表结构,则类似于遍历 Python 字典。

  示例:对于 Series 循环

s = pd.Series(data=np.arange(5),index=['rank'+str(i) for i in np.arange(1,6)])
print(s)
print("-"*10)
for ele in s:
    print(ele)

  输出结果

rank1    0
rank2    1
rank3    2
rank4    3
rank5    4
dtype: int32
----------
0
1
2
3
4

  示例:对于 DataFrame 循环

df = pd.DataFrame({
    'col1':np.arange(4),
    'col2':np.arange(4,8),
    'col3':np.arange(8,12),
},index=['row'+str(i) for i in np.arange(1,5)])
print("原始数据为:\n",df)
print("遍历:")
for col in df:
    print(col)

  输出结果:输出的是 DataFrame 的列名。

原始数据为:
       col1  col2  col3
row1     0     4     8
row2     1     5     9
row3     2     6    10
row4     3     7    11
遍历:
col1
col2
col3

2 内置迭代方法

  如果想要遍历 DataFrame 的每一行,我们下列函数:

  • items():以键值对 (key,value) 的形式遍历;
  • iteritems():以键值对 (key,value) 的形式遍历;
  • iterrows():以 (row_index,row) 的形式遍历行;
  • itertuples():使用已命名元组的方式对行遍历。

  下面对上述函数做简单的介绍:

2.1 items()函数

  示例:

for label ,content in df.items():
    print(label)
    print(content)

  输出结果:

col1
row1    0
row2    1
row3    2
row4    3
Name: col1, dtype: int32
col2
row1    4
row2    5
row3    6
row4    7
Name: col2, dtype: int32
col3
row1     8
row2     9
row3    10
row4    11
Name: col3, dtype: int32

2.2 iteritems()函数

  示例:

for key,item in df.iteritems():
    print(key)
    print(item)

  输出结果:

col1
row1    0
row2    1
row3    2
row4    3
Name: col1, dtype: int32
col2
row1    4
row2    5
row3    6
row4    7
Name: col2, dtype: int32
col3
row1     8
row2     9
row3    10
row4    11
Name: col3, dtype: int32

2.3 iterrows()

  该方法按行遍历,返回一个迭代器,以行索引标签为键,以每一行数据为值。

  示例如下:

print("原始数据:\n",df)
print("通过行遍历:")
for row_index,row in df.iterrows():
    print (row_index)
    print(row)

  输出结果:

原始数据:
       col1  col2  col3
row1     0     4     8
row2     1     5     9
row3     2     6    10
row4     3     7    11
通过行遍历:
row1
col1    0
col2    4
col3    8
Name: row1, dtype: int32
row2
col1    1
col2    5
col3    9
Name: row2, dtype: int32
row3
col1     2
col2     6
col3    10
Name: row3, dtype: int32
row4
col1     3
col2     7
col3    11
Name: row4, dtype: int32

  注意:iterrows() 遍历行,其中 row1,row2,row3,row4 是行索引而 col1,col2,col3 是列索引。

2.4 itertuples()函数

  itertuples() 同样将返回一个迭代器,该方法会把 DataFrame 的每一行生成一个元组。

  示例如下:

print("原始数据:\n",df)
print("通过行遍历1:")
for row in df.itertuples():
    print(row)
print("通过行遍历2:")
for row in df.itertuples():
    for rowdata in row:
        print(rowdata,end='\t')
    print()

  输出结果:

原始数据:
       col1  col2  col3
row1     0     4     8
row2     1     5     9
row3     2     6    10
row4     3     7    11
通过行遍历1:
Pandas(Index='row1', col1=0, col2=4, col3=8)
Pandas(Index='row2', col1=1, col2=5, col3=9)
Pandas(Index='row3', col1=2, col2=6, col3=10)
Pandas(Index='row4', col1=3, col2=7, col3=11)
通过行遍历2:
row1	0	4	8	
row2	1	5	9	
row3	2	6	10	
row4	3	7	11	

2.5 迭代修改原始值

  如果在迭代过程中修改元素值,会影响原对象,这一点需要大家注意。
  看一组简单的示例:

print("原始数据:\n",df)
print("通过行遍历:")
for row_index,row in df.iterrows():
    print (row_index)
    print(row)
print('修改1:')
for row_index,row in df.iterrows():
    row['col4']='100'
print(df)
print('修改2:')
for row_index,row in df.iterrows():
    row['col1']='100'
print(df)

  输出结果:

原始数据:
       col1  col2  col3
row1     0     4     8
row2     1     5     9
row3     2     6    10
row4     3     7    11
通过行遍历:
row1
col1    0
col2    4
col3    8
Name: row1, dtype: int32
row2
col1    1
col2    5
col3    9
Name: row2, dtype: int32
row3
col1     2
col2     6
col3    10
Name: row3, dtype: int32
row4
col1     3
col2     7
col3    11
Name: row4, dtype: int32
修改1:
      col1  col2  col3
row1     0     4     8
row2     1     5     9
row3     2     6    10
row4     3     7    11
修改2:
      col1  col2  col3
row1   100     4     8
row2   100     5     9
row3   100     6    10
row4   100     7    11