Python Forum
How to iterate over the pandas rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate over the pandas rows
#1
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
Reply
#2
(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)
Reply
#3
Just something to think about StackOverflow: How to iterate over rows in a DataFrame in Pandas? Don't.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 697 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 839 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,634 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  How to combine multiple rows of strings into one using pandas? shantanu97 1 3,151 Aug-22-2021, 05:26 AM
Last Post: klllmmm
  Python Pandas: How do I sumproduct by rows with an if condition? JaneTan 2 5,396 Jul-13-2021, 11:36 AM
Last Post: jefsummers
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,371 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,116 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,124 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 6,828 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to diff pandas rows and modify column value Mekala 1 1,699 Sep-18-2020, 12:38 PM
Last Post: Mekala

Forum Jump:

User Panel Messages

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