dataframe 按行 遍历
时间: 2023-10-13 10:03:04 浏览: 82
对于DataFrame按行遍历,可以使用iterrows()方法来实现。iterrows()方法会返回一个迭代器对象,该对象包含每一行的索引和数据。可以使用for循环来逐行处理数据。下面是一个示例代码:
```python
import pandas as pd
# 示例数据
data = {'name': ['刘一', '陈二', '张三', '李四', '王五'],
'age': [18, 19, 20, 21, 22],
'height': [175, 176, 177, 178, 179]}
index = ['0001', '0002', '0003', '0004', '0005']
df = pd.DataFrame(data=data, index=index)
df.index.name = 'id'
# 按行遍历
for index, row in df.iterrows():
print("Index:", index)
print("Row:", row)
# 输出每行的索引值和对应的数据
```
这段代码会按行遍历DataFrame,并输出每行的索引值和对应的数据。你可以根据实际需求修改输出的内容或进行其他操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Dataframe按行按列遍历的几种方式](https://blog.csdn.net/weixin_48419914/article/details/120328571)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [pandas.DataFrame按行遍历和按列遍历](https://blog.csdn.net/lly1122334/article/details/121775416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文