打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iterrows(),

https://blog.csdn.net/weixin_39570505/article/details/112925109

如果要遍历DataFrame以对每行执行一些操作, 则可以在Pandas中使用iterrows()函数。

Pandas使用三个函数来迭代DataFrame的行, 即iterrows(), iteritems()和itertuples()。

用Pandas迭代行

iterrows()负责遍历DataFrame的每一行。它返回一个迭代器, 该迭代器包含作为系列的每一行的索引和数据。

我们具有下一个函数来查看迭代器的内容。

此函数返回每个索引值以及包含每一行数据的序列。

iterrows()-用于迭代(索引, 系列)对的行。

iteritems()-用于迭代(键, 值)对。

itertuples()-用于迭代作为namedtuples的行。

产量:

index:返回行的索引和MultiIndex的元组。

data:以系列的形式返回该行的数据。

it:返回在框架的行上进行迭代的生成器。

例1

import pandas as pd

import numpy as np

info = pd.DataFrame(np.random.randn(4, 2), columns = ['col1', 'col2'])

for row_index, row in info.iterrows():

print (row_index, row)

输出

0 name John

degree B.Tech

score 90

Name: 0, dtype: object

1 name Smith

degree B.Com

score 40

Name: 1, dtype: object

2 name Alexander

degree M.Com

score 80

Name: 2, dtype: object

3 name William

degree M.Tech

score 98

Name: 3, dtype: object

例2

# importing pandas module

import pandas as pd

# making data frame from csv file

data = pd.read_csv("aa.csv")

for i, j in data.iterrows():

print(i, j)

print()

输出

0 Name Hire Date Salary Leaves Remaining 0 John Idle 03/15/14 50...

Name: 0, dtype: object

1 Name Hire Date Salary Leaves Remaining 1 Smith Gilliam 06/01/15 65000...

Name: 1, dtype: object

2 Name Hire Date Salary Leaves Remaining 2 Parker Chapman 05/12/14 45000.0 ...

Name: 2, dtype: object

3 Name Hire Date Salary Leaves Remaining 3 Jones Palin 11/01/13 700...

Name: 3, dtype: object

4 Name Hire Date Salary Leaves Remaining 4 Terry Gilliam 08/12/14 4800...

Name: 4, dtype: object

5 Name Hire Date Salary Leaves Remaining 5 Michael Palin 05/23/13 66000...

Name: 5, dtype: object

————————————————

版权声明:本文为CSDN博主「weixin_39570505」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_39570505/article/details/112925109

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Pandas DataFrame的基本属性详解
pandas中的字符串处理函数
python pandas 数据处理
Pandas中文官档~基础用法6
python基础1| 索引与切片
如何高效地对比处理 DataFrame 的两列数据
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服