Search code snippets, questions, articles...

Loop through DataFrame rows in python pandas

DataFrame iterrows() method can be used to loop through or iterate over Dataframe rows. You can get the value of a row by its column name in each iteration.
import pandas as pd

df = pd.DataFrame({
    'column_1': ['John', 'Eric', 'Rick'],
    'column_2': [100, 110, 120]
})

for index, row in df.iterrows():
    print(row['column_1'], row['column_2'])

#prints
# John 100
# Eric 110
# Rick 120

Search Index Data (The code snippet can also be found with below search text)

iterate DataFrame rows in python pandas
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet