pandas按行遍历
时间: 2023-08-18 21:04:50 浏览: 105
Numpy&pandas(四)–分组计算
可以使用`iterrows()`方法来按行遍历Pandas DataFrame。这个方法会返回一个由索引和Series对象组成的元组。你可以使用for循环来遍历DataFrame的每一行,并使用索引和Series对象来访问每一行的值。以下是一个示例代码:
```python
import pandas as pd
df = pd.DataFrame({'species': ['bear', 'bear', 'marsupial'],
'population': [1864, 22000, 80000]},
index=['panda', 'polar', 'koala'])
for index, row in df.iterrows():
print(index, row['species'], row['population'])
```
这段代码会按行遍历DataFrame,并打印出每一行的索引、species列和population列的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [pandas逐行/列 遍历Dataframe的三种方式](https://blog.csdn.net/qq_57313910/article/details/128080882)[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/devid008/article/details/131212904)[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 ]
阅读全文