0

How can I get my code to iterate over all rows in my df and save it each value? At the moment it only saves the first row to my person object and ignores the others.

if __name__ == "__main__":

    col_list = ['FirstName', 'LastName', 'DateOfBirth', 'Email', 'Phone', 'Address', 'Country']

    df = create_cpr(read_data(f'{path}', col_list), symbol)
    print('df=', df)

    for person in df.values.tolist():
        firstname = person[0] 
        lastname = person[1]
        email = person[2] 
        cpr  = person[7]  

    person_xml = create_xml(firstname, lastname, email, cpr)
aneroid
  • 12,983
  • 3
  • 36
  • 66
TBI
  • 77
  • 1
  • 8
  • https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html is what you want. `for row in df.iterrows()` – Paul Brennan Jan 11 '21 at 12:51
  • What's the problem in your approach of converting it to list? – bigbounty Jan 11 '21 at 12:53
  • 1
    Does this answer your question? [How to iterate over rows in a DataFrame in Pandas](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Antonis Christofides Jan 11 '21 at 12:54
  • 1
    It looks like you are overwriting your firstname, lastname, email, and cpr variables in the for loop. – Divakar Patil Jan 11 '21 at 12:54
  • @DivakarPatil How do I not do that? – TBI Jan 11 '21 at 13:51
  • @PaulBrennan not really, I need to send off response with the select data but it. only sends off one row – TBI Jan 11 '21 at 14:41
  • 1
    @Toni It will depend on your xml structure and create_xml() method. It will be helpful to answer your question if you can share the xml structure that you are trying to create. – Divakar Patil Jan 11 '21 at 14:48

0 Answers0