Python Forum

Full Version: How to iterate over the pandas rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to iterate through the "Pandas DataFrame" rows and while the "last_day <=day_set".

Condition1: Iterate over the rows of the first column.
sub_condition: on each iteration, check and break the iteration if day_set<=days_last.

I use below code, but not complete,

Kindly someone help.

import pandas as pd
import csv
input=r'D:\PythonCodes\correctiofactordata.csv'
df=pd.read_csv(input)
increment=3
prev_day=0
day_set=5
days_last=df[-1:]['Days_elapsed']
for i, row in df.iterrows():    
        data_catch=df.loc[(df['Days_elapsed'] >= prev_day) & (df['Days_elapsed'] <= day_set)]
        if((data_catch.shape[0])>0):
            print('Enough data')
        else:
            print('not enough data')
        print(prev_day),print(day_set)
        prev_day+=5
        day_set+=5
    
        print('-------------------')      
input data:

Time_golden	Golden	is_golden	Days_elapsed	Factor
20-03-2019 10:24	98.6	golden	0.0	1.0
20-03-2019 11:10	97.0	golden	0.15	1.3
20-03-2019 13:13	96.0	golden	0.53	1.5
21-03-2019 13:43	95.0	golden	1.03	0.95
23-03-2019 10:37	94.6	golden	1.2	0.96
23-03-2019 18:43	93.0	golden	1.6	1.0
24-03-2019 22:19	92.0	golden	2.53	1.3
25-03-2019 09:23	90.0	golden	2.69	1.5
26-03-2019 11:42	89.0	golden	2.9	0.95
27-03-2019 20:32	87.3	golden	3.0	0.96
27-03-2019 23:42	86.0	golden	3.21	1.5
28-03-2019 00:52	84.0	golden	3.62	0.95
28-03-2019 03:40	82.3	golden	3.96	0.96
21-03-2019 10:34	83.0	notgolden	4.23	1.0
24-03-2019 16:37	80.0	notgolden	5.2	1.3
24-03-2019 21:42	73.0	notgolden	5.6	0.95
27-03-2019 21:02	60.0	notgolden	5.69	0.96
28-03-2019 02:42	53.0	notgolden	6.1	1.0
20-03-2019 10:24	98.6	golden	6.66	1.3
20-03-2019 11:10	97.0	golden	6.987	1.5
20-03-2019 13:13	96.0	golden	7.03	0.95
21-03-2019 13:43	95.0	golden	7.0236	0.96
23-03-2019 10:37	94.6	golden	7.26	1.5
23-03-2019 18:43	93.0	golden	8.5	0.95
24-03-2019 22:19	92.0	golden	8.62	0.96
25-03-2019 09:23	90.0	golden	9.6	1.0
26-03-2019 11:42	89.0	golden	11.02	1.3
27-03-2019 20:32	87.3	golden	11.63	1.3
27-03-2019 23:42	86.0	golden	12.35	1.0
(Jun-03-2019, 11:02 PM)SriMekala Wrote: [ -> ]I use below code, but not complete,
is not a good description of the problem at hand.

The code above gives
Error:
KeyError: 'Days_elapsed'
Please show error trackbacks.

You need to set the correct sep for df=pd.read_csv(input)
There is no error if I run the same code,

I want to iterate over rows while below condition satisfies.
Condition: "day_set<=days_last"