0

I'm attempting to process a dataframe one row at a time using iloc. I have a sorted frame read in from a Database:

branch  animal  owner
00177   cat     Dave
00177   lion    Peter
00177   dog     Dave
00200   dog     Mary
00200   dog     Fred
00300   horse   Doug
00400   cat     Dave

I want to process each group of branch, so I thought if I compared each grpnum with the next one. like this:

for R in 0 to 7
    while [df.iloc[[R],[0]] == df.iloc[[R+1],[0]]
        do something
        R = R + 1

Except I got this error:

ValueError: Can only compare identically-labeled DataFrame objects

Can I not process a DataFrame this way? How would one process a DataFrame row by row? I'm not tied to using a dataframe object, it was just convenient. This example shows Branch with 4 distinct values. There are actually over a thousand different values, and the same branch can contain up to thirty records and as few as one.

C0ppert0p
  • 634
  • 2
  • 7
  • 23
  • 1
    Not sure if understand, can you add expected output? Do you need compare values between `groups` by `branch` column? – jezrael Mar 22 '18 at 08:55
  • if you want to compare to the next row, look into the `DataFrame.shift` function. perhaps even `groupby` is what you really want – Maarten Fabré Mar 22 '18 at 11:10

1 Answers1

1

When you do this sort of comparison the index is also compared

>>> df.iloc[[1],[0]]
branch
1  00177
>>> df.iloc[[0],[0]]
branch
0  00177

which in this case are 1 and 0 respectively

You can look for more detail here

You can try a different approach to your problem like

for i in range(7):
    if df["branch"][i] == df["branch"][i+1]:
        do something
    else:
        pass
Jack Daniels
  • 123
  • 1
  • 2
  • 13