Python Forum
Adding loop results as rows in dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding loop results as rows in dataframe
#1
How do I make this for loop print the results as respective rows of the same dataframe. I tried this code but it doesn't seem to be working.

list2=[(('ATM',), ('ROA',)), (('ATM',), ('ROE',)), (('ATM',), ('NIM',)), (('ATM',), ('ROA', 'ROE'))]
tm_final = {0: 3, 1: 6, 2: 4, 3: 2, 4: 0}
totalrows=sum(tm_final[i]>0 for i in tm_final)
df=pd.DataFrame(columns=['A','B','C'])

for i in tm_final:
  if tm_final[i]>0:
    for j in range(totalrows):
      df.iloc[j,0]=i
      df.iloc[j,1]=str(list2[i])
      df.iloc[j,2]=tm_final[i]
    print(df)
Reply
#2
Are you expecting this output?

list2=[(('ATM',), ('ROA',)), (('ATM',), ('ROE',)), (('ATM',), ('NIM',)), (('ATM',), ('ROA', 'ROE'))]
tm_final = {0: 3, 1: 6, 2: 4, 3: 2, 4: 0}
totalrows=sum(tm_final[i]>0 for i in tm_final)
df=pd.DataFrame(columns=['A','B','C'])
 
for i in tm_final:
  if tm_final[i]>0:
      df=df.append({'A':i,'B':list2[i],'C':tm_final[i]},ignore_index=True)
      print(df)
Reply
#3
(May-09-2020, 10:25 AM)anbu23 Wrote: Are you expecting this output?

list2=[(('ATM',), ('ROA',)), (('ATM',), ('ROE',)), (('ATM',), ('NIM',)), (('ATM',), ('ROA', 'ROE'))]
tm_final = {0: 3, 1: 6, 2: 4, 3: 2, 4: 0}
totalrows=sum(tm_final[i]>0 for i in tm_final)
df=pd.DataFrame(columns=['A','B','C'])
 
for i in tm_final:
  if tm_final[i]>0:
      df=df.append({'A':i,'B':list2[i],'C':tm_final[i]},ignore_index=True)
      print(df)

Thank you! That worked. Only thing is that the print(df) should be out of the loop, so that it only prints it once.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 299 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,655 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,400 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Help adding a loop inside a loop Extra 31 4,520 Oct-23-2022, 12:16 AM
Last Post: Extra
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 828 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Help add for loop results in a list paulo79 4 1,541 Mar-09-2022, 05:49 PM
Last Post: deanhystad
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,313 Jan-21-2022, 06:24 PM
Last Post: mcva
Question How can I merge several images via loop using the information of a dataframe? noahverner1995 1 1,427 Dec-31-2021, 05:03 AM
Last Post: noahverner1995
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,629 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  for loop in dataframe in pandas Paulman 7 2,748 Dec-02-2021, 12:15 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020