Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to loop through Rows
#1
Hi,

[python]I have belwo DataFrame:

Input1:
first_name	pre_score	mid_score	post_score
UAT_F3	     4	        25	        5
UAT_F5	     24	        94	        43
UAT_F2	     31	        57	        23
UAT_F6    	 2	        62	        23
UAT_F8	     3	        70	        51

Input2:
first_name	pre_score	mid_score	post_score
UAT_F3	     4	        25	        5
UAT_F5	     24	        94	        43
UAT_F2	     31	        57	        23
UAT_F6    	 2	        62	        23
UAT_F8	     3	        70	        51

I want to loop through the rows of the input1&Input2 data and on each iteration, copy the current rows from Input2(first_name store &pre_score) and previous row =(i-1th row) from Input1, and do something. 
I use the below code, but I could not be able to access & store.

[python]
newData=[]
for index, row in Input1.iterrows():
    print(index)
    tempData1=Input1.loc[[index-1,:]]
    tempData2=Input2.loc[[index,:]]
    temp=[tempData1,tempData2]
    newData.append(temp)
Desired Output: 
first_name	pre_score	mid_score	post_score	    
UAT_F5	    4	        25	        5  
UAT_F2	    24	        94	        43
UAT_F6      31	        57	        23
UAT_F8	    2	        62	        23

Kindly someone help, and many thanks in advance,
[/python]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,125 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,155 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Adding loop results as rows in dataframe Shreya10o 2 2,201 May-09-2020, 11:00 AM
Last Post: Shreya10o

Forum Jump:

User Panel Messages

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