how to create a dataframe in the for loop?

A simple way if you only need the dataframe after the loop is completed is to append the data to a list of lists and then convert to a dataframe. Caveat: Responsibility is on you to make sure the list ordering matches the columns, so if you change your columns in the future you have to reposition the list.

list_of_rows = []
for elements in tree.findall('{http://xmlns.kztc-cits/sign}payment'):
   list_of_rows.append([
      tax, tax_name_ru, kbk, kbk_name_ru, paynum, paytype,entry_type, writeoffdate, summa])

df = pd.DataFrame(columns=columns, data=list_of_rows)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top