Iterating over the rows of a Pandas DataFrame is commonly encountered when manipulating tabular data. This article explores two methods to accomplish this task and sheds light on the composition of row objects.
Pandas provides an efficient DataFrame.iterrows generator that returns both the index and row as a Series for each observation. This method allows direct access to column values using the row's index:
import pandas as pd df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]}) for index, row in df.iterrows(): print(row['c1'], row['c2'])
This snippet outputs:
10 100 11 110 12 120
While iterating over Pandas objects is generally a convenient approach, it can be slow compared to vectorized operations. For maximum performance, consider alternative techniques such as:
Beyond iterrows(), Pandas offers other row iteration methods such as:
The above is the detailed content of How Can I Efficiently Iterate Through Pandas DataFrame Rows?. For more information, please follow other related articles on the PHP Chinese website!