-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?

3
  • 2
    That is not a CSV file. Please provide some sample data (make a fake dataset), and an expected result Jun 1, 2022 at 23:43
  • Answer if you're using Pandas.
    – PGHE
    Jun 1, 2022 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. Jun 1, 2022 at 23:56

1 Answer 1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.