pandas按行遍历Dataframe,iterrows()方法

本文通过实例演示了如何使用Python的Pandas库初始化DataFrame,并展示了如何遍历DataFrame的行和获取特定列的数据。同时,文章还对比了筛选后索引不连续情况下的数据遍历效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初始化一个Dataframe

import pandas as pd

List=[['Yes', None, None, None],
 [None, None, None, None],
 ['Yes', None, None, None],
 [None, None, None, None]]

df=pd.DataFrame(List,columns=['A','B','C','D'])

结果:

      A     B     C     D
0   Yes  None  None  None
1  None  None  None  None
2   Yes  None  None  None
3  None  None  None  None

遍历行,输出索引,A列内容:

for index,row in df.iterrows():
    print(index,row['A'])

输出:

0 Yes
1 None
2 Yes
3 None

如果,数据是筛选后,索引不是连续的,

     A     B     C     D
0  Yes  None  None  None
2  Yes  None  None  None
for index,row in temp.iterrows():
    print(index,row['A'])

输出:

0 Yes
2 Yes

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值