Home > Software engineering >  How to iterate through rows of a DataFrame?
How to iterate through rows of a DataFrame?

Time:06-02

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?

CodePudding user response:

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.

CodePudding user response:

i advice you see this article to solve it https://www.listendata.com/2019/06/pandas-read-csv.html

  • Related