-1

I have this code:

df = pd.DataFrame(data, columns = ['Name','New_addy','PHONE1','PHONE2','EMAIL'])

def QuestioningMech(df):
    for x in df:
        print(df[x])

So I am trying to iterate through the DataFrame, but this code iterates over columns. How do I iterate over rows instead?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    That is not a CSV file. Please provide some sample data (make a fake dataset), and an expected result – Mad Physicist Jun 01 '22 at 23:43
  • [Answer](https://stackoverflow.com/a/55557758/5400385) if you're using Pandas. – PGHE Jun 01 '22 at 23:45
  • If you want to iterate through a Pandas DataFrame, then say so. The CSV file format is a totally separate thing that has nothing to do with the code shown. I am removing that tag. – Karl Knechtel Jun 01 '22 at 23:56

1 Answers1

0

pandas.DataFrame.iterrows iterates rows. So,

def QuestioningMech(df):
    for row in df.iterrows():
        print(row)


df = pd.DataFrame(data=[[1,2,3], [4,5,6], [7,8,9]], columns=['A', 'B', 'C'])
QuestioningMech(df)

This is not be the most efficient way to handle a dataframe, depending on what you plan to do with the data.

tdelaney
  • 73,364
  • 6
  • 83
  • 116