Lesson9——Pandas iteration遍历
2023-09-14 09:08:53 时间
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
相关文章
- 在Pandas Dataframe中遍历行的不同方法
- MyBatis 批量操作、集合遍历-foreach
- LeetCode(106):从中序与后序遍历序列构造二叉树
- jquery遍历:数组、对象、json
- LeetCode(94):二叉树的中序遍历
- C++实现的二叉树创建和遍历,超入门邻家小女也懂了
- Atitit table的读取与遍历 jquery jsoup 目录 1. atitit.gui控件的定位与选择器1 2. 读取表格流程 遍历表格table1 3. W3cdom标准化1
- 华为OD机试 - 二叉树中序遍历(Java & JS & Python)
- 华为OD机试 - 完全二叉树非叶子部分后序遍历(Java & JS & Python)
- Python编程小技巧:遍历py文件自动去掉所有注释仅剩代码函数(有部分可能导致函数代码顺序错乱)
- 面试题 01.09. 字符串轮转-遍历字符串
- 1233. 删除子文件夹-字符串排序+遍历
- vue3中如何通过遍历传入组件名称动态创建多个component 组件
- 再说linux中的rm mv 遍历执行多个文件的操作: find + xagrs
- 算法编程 dfs 从先序和中序遍历还原二叉树
- 07-循环遍历