for循环dataframe列数据
时间: 2023-09-04 12:06:48 浏览: 146
Python通过两个dataframe用for循环求笛卡尔积
可以使用DataFrame的iterrows()方法来遍历行数据,然后在for循环中取出每一行数据中的列数据,例如:
```python
import pandas as pd
data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'gender': ['F', 'M', 'M']}
df = pd.DataFrame(data)
for index, row in df.iterrows():
name = row['name']
age = row['age']
gender = row['gender']
print(name, age, gender)
```
输出结果为:
```
Alice 25 F
Bob 30 M
Charlie 35 M
```
阅读全文