0

I know this question has been asked many times but I have tried almost all methods I am trying to iterate over rows in DataFrame using following code.

import pandas as pd

df = pd.read_csv(r"file.csv")

for row in df:
    print(row)

there is only one column in csv file and it only prints header of file. I have also used .items() , .iterrows() and .itertuples(), all these methods give tuples as output which I don't need. Further task I am trying to do in loop only accepts output from for row in df:. kindly help that how can I print actual rows this way rather than just header name. Thank you.

3
  • You can iterate as for row in df[col_name].to_list(): Commented Dec 15, 2020 at 7:30
  • Use for _, row in df.iterrows() Commented Dec 15, 2020 at 7:45
  • 1
    @Ajay This is the answer can you please post in answer section so I can mark it. Thank you for your time and effort Commented Dec 15, 2020 at 7:48

2 Answers 2

2

The df.iterrows() method iterates over each row of a dataframe, returning a tuple of (row_number, row_data). So to just get the row data, you can unpack each tuple as part of the iterration and only use the second part:

for num, row in df.iterrows():
    print(row)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this, using the specific column on which you want to iterate

for row in df[col_name].to_list():

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.