Introduction to Pandas
Chapter 4: Accessing DataFrame rows and columns
Iterating over columns and rows
Iterating over columns
Iterating over a DataFrame
object gives you the column names
>>> for col in df:
... print(col)
...
#
Type 1
Type 2
Total
HP
Attack
Defense
Sp. Atk
Sp. Def
Speed
Generation
Legendary
Iterating over rows
To iterate over rows, you have several choices: .iteritems()
, .iterrows()
, .itertuples()
.
Try to figure out what the difference between these three choices are! What does each of them actually return? (They are all different!)
for (key, value) in df.iteritems():
print(key, value)
for (row_index, row) in df.iterrows():
print(row_index, row)
for row in df.itertuples():
print(row)