0
votes

I have a dataframe. enter image description here

distdf=pd.DataFrame(dist)
for i in range(len(distdf)):
    if distdf.loc[i]==distdf.loc[i+1]:
        print("true")
    else:
        print('no')

but i have a error.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How can i fix it?

3

3 Answers

1
votes

Your code failed because distdf.loc[i] == distdf.loc[i+1] attempts to compare whole rows, i.e. two Series objects.

Some impovement would be if you wrote: if distdf.loc[i, 'DISTINGUISH']==distdf.loc[i+1, 'DISTINGUISH']:, i.e. compared DISTINGUISH elements in each row (current and the next).

But this code would also fail on the last turn of the loop, because when you "stay" on the last row, there is no next row, so then KeyError exception is raised.

There is a simpler, more pandasonic approach to get a list of your values, without any loop.

Assume that your DataFrame (distdf) contains:

    DISTINGUISH
0             1
1             1
2             2
3             2
4             3
5             3
6            32
7            32
8            33
9            33
10           34
11           34

As you want to operate on its only column, to keep the code short, let's save it under col0:

col0 = distdf.iloc[:, 0]

Then, to get your list of values, without any loop, run:

np.where(col0 == col0.shift(-1), 'true', 'no')

The result, for the above data, is:

array(['true', 'no', 'true', 'no', 'true', 'no', 'true', 'no', 'true',
       'no', 'true', 'no'], dtype='<U4')
0
votes

Despite the fact that you only have one column, distdf.loc[i] is still a Pandas Series object and not just the integer contained in the only column, thus the error.

What you could do is comparing the value of each cell of the single column using .values to avoid getting this error :

# you also need to use len(distdf)-1 and not len(distdf)
for i in range(len(distdf)-1):
    if distdf.loc[i].values == distdf.loc[i+1].values:
        print("true")
    else:
        print('no')
0
votes
dist =[1,2,3,4,5,6]
distdf = pd.DataFrame(dist)
for i in range(len(distdf)-1):
    res = distdf.loc[i].values==distdf.loc[i].values
    if True in res:
        print("true")
    else:
        print('no')