Iterate over rows and columns pandas DataFrame
Example of iterrows and itertuples:
import pandas as pd employees = pd.DataFrame({ 'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'], 'Name': ['John', 'Doe', 'William', 'Spark', 'Mark'], 'Occupation': ['Chemist', 'Statistician', 'Statistician', 'Statistician', 'Programmer'], 'Date Of Join': ['2018-01-25', '2018-01-26', '2018-01-26', '2018-02-26', '2018-03-16'], 'Age': [23, 24, 34, 29, 40]}) print("\n Example iterrows \n") for index, col in employees.iterrows(): print(col['Name'], "--", col['Age']) print("\n Example itertuples \n") for row in employees.itertuples(index=True, name='Pandas'): print(getattr(row, "Name"), "--", getattr(row, "Age"))
C:\python\pandas examples>python example7.py Example iterrows John -- 23 Doe -- 24 William -- 34 Spark -- 29 Mark -- 40 Example itertuples John -- 23 Doe -- 24 William -- 34 Spark -- 29 Mark -- 40 C:\python\pandas examples>
2018-10-27T12:57:15+05:30
2018-10-27T12:57:15+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution