Reputation: 61
I've loaded a CSV file using Pandas. The CSV file has 4000 rows. It loaded correctly. When printing out the data frame, all 4000 rows are printed. But when I iterate through the rows using a "for" loop, it only prints the first row in the file.
This is my code:
import pandas as pd
df = pd.read_csv('EX2_EM_GMM.csv')
for sample in df:
print sample
An Ideas? Thanks!
Upvotes: 2
Views: 9861
Reputation: 2500
In your case I think following examples provide solution and also providing time of execution. for this amount of rows I will use itertuples()
import pandas as pd
import numpy as np
di = {k:np.random.randn(4000) for k in ["a", "b", "c", "d"]}
df = pd.DataFrame(di)
for row in df.itertuples():
print row
%timeit [row for row in df.itertuples()]
%timeit [row for row in df.iterrows()]
Upvotes: 2
Reputation: 2373
Use iterrows() to loop through each of the rows. The default iteration will just show the columns.
for sample in df.iterrows():
print sample
Upvotes: 0
Reputation: 1441
For iterating over DataFrame
rows you can use .iterrows() function.
for index, row in df.iterrows():
# Process each row
Upvotes: 4