There are a few different ways to loop through a Pandas DataFrame. One option is to use the iterrows()
method, which returns an iterator yielding index and row data for each row. Here’s an example of how to use iterrows()
to loop through a DataFrame and print the value of each cell:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Loop through the DataFrame and print the value of each cell
for index, row in df.iterrows():
print(row['A'], row['B'], row['C'])
Output:
1 4 7
2 5 8
3 6 9
Another option is to use the apply()
method, which applies a function to each row or column of the DataFrame. Here’s an example of how to use apply()
to loop through the rows of a DataFrame and print the value of each cell:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Define a function to apply to each row
def print_row(row):
print(row['A'], row['B'], row['C'])
# Apply the function to each row of the DataFrame
df.apply(print_row, axis=1)
Output:
1 4 7
2 5 8
3 6 9
You can also use a for
loop to loop through the rows of the DataFrame and access the values using the iloc
indexer. Here’s an example:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Loop through the rows of the DataFrame
for i in range(len(df)):
row = df.iloc[i]
print(row['A'], row['B'], row['C'])
Output:
1 4 7
2 5 8
3 6 9
It’s worth noting that looping through a DataFrame can be inefficient, especially for large datasets. In many cases, it’s more efficient to use vectorized operations or the built-in methods provided by Pandas.