Python Forum

Full Version: How to loop through Rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]