Tal Geller
Tal Geller

Reputation: 61

When iterating through CSV file and printing the rows, only the first row gets printed

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

Answers (3)

n1tk
n1tk

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()

itertuples() and iterrows()

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()]

output sample result of execution time

Upvotes: 2

jeff carey
jeff carey

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

amin
amin

Reputation: 1441

For iterating over DataFrame rows you can use .iterrows() function.

for index, row in df.iterrows():
    # Process each row

Upvotes: 4

Related Questions