2

I have a dataframe similar to below,

Date            A  B
2017-01-01      1  1
2017-01-02      2  2
2017-01-03      1  2

For each column, I want to test the condition A==B for each name in the groupby statement. If this condition does not hold true, I want to print "Condition ends at 2017-01-03." I'm not sure how to loop through each row in this dataframe. I'm envisioning something like this code, although I know I am not itterating through the correct thing:

for i in df.Date:
    if df.A == df.B:
        continue
    if df.A != df.B:
        print ("Condition ends at", i)
amarynets
  • 1,765
  • 10
  • 27
Erin
  • 81
  • 1
  • 2
  • 4
  • Not really the answer, but you could skip the checking the equality of the A and B, and only keep the “not equal” check. – Fontinalis Sep 27 '17 at 20:36

3 Answers3

1

Please take a look at this.

You would be iterating through each row and then accessing the appropriate column as a key, in this way you can compare column to each other in each row

jshbrmn
  • 1,737
  • 3
  • 22
  • 52
0

You could do it like this:

print 'Condition ends at', (df.A.values != df.B.values).argmax()
Evan Nowak
  • 895
  • 4
  • 8
0

You could use the nice pythonic syntax:

print next([d for d in dates if d.A != d.B], None)

The None would be the default value if none is found

maor10
  • 1,645
  • 18
  • 28