The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a.item(), a.any() or a.all()

Asked

Viewed 1,343 times

3

I am a layperson in programming, in the stackoverflow community and I am not able to solve the following problem:

Create two lists (or if you prefer 2 columns in a New Dataframe) in which you search in a Dataframe the successor and the predecessor of the elements that are another Datatframe(reference). I made a code (I am open to new solutions) and is accusing an error message that is in the title.

antecessor = []
sucessor = []

for elemento in x[0]:
    i = 0
    while i < y[0].size:
        if elemento < y[0].iloc[i:i+1]:
            sucessor.append(y[0].iloc[i:i+1])
            antecessor.append(y[0].iloc[i-1:i])
            break
    i += 1 

Obs:

i. The elements found in the Dataframe X and Y columns are float-type

ii. The number of Dataframe X elements is less than Dataframe Y.

Thanks in advance for all your help and attention.

Sincerely yours truly, Wendel de Oliveira

x.dat (2 columns)

0.509054 0.0767 0.355404 0.0769 0.279519 0.0771 0.219150 0.0773 0.176476 0.0775 0.151162 0.0777 0.139228 0.0779 0.117106 0.0781 0.108191 0.0783 0.095156 0.0785

y. dat (4 columns)

0.0893015 1.00584 0.0725685 -1.83063 0.0938015 1.00614 0.0642253 -1.87747 0.0983015 1.00641 0.0556713 -1.9243 0.102801 1.00664 0.0469066 -1.97114 0.107301 1.00684 0.0379312 -2.01797 0.111801 1.00699 0.0287449 -2.06481 0.116301 1.00709 0.0193479 -2.11165 0.120801 1.00716 0.00974018 -2.15848 0.125301 1.00718 -7.83241e-05 -2.20532 0.129801 1.00716 -0.0101076 -2.25215 0.134301 1.00709 -0.0203476 -2.29899 0.138801 1.00697 -0.0307984 -2.34582 0.143301 1.00681 -0.0414599 -2.39266 0.147801 1.0066 -0.0523322 -2.43949 0.152301 1.00634 -0.0634153 -2.48632 0.156801 1.00603 -0.0747091 -2.53316 0.161301 1.00567 -0.0862136 -2.57999 0.165801 1.00525 -0.0979289 -2.62682 0.170301 1.00479 -0.109855 -2.67365 0.174801 1.00426 -0.121992 -2.72048 0.179301 1.00369 -0.134339 -2.76731 0.183801 1.00305 -0.146897 -2.81414 0.188301 1.00236 -0.159666 -2.86097 0.192801 1.00162 -0.172646 -2.90779 0.197301 1.00081 -0.185836 -2.95462 0.201801 0.999944 -0.199237 -3.00144 0.206301 0.999017 -0.212849 -3.04826 0.210801 0.998028 -0.226672 -3.09508 0.215301 0.996977 -0.240705 -3.14189 0.219801 0.995861 -0.254949 -3.18871 0.224301 0.994682 -0.269403 -3.23552 0.228801 0.993436 -0.284068 -3.28233 0.233301 0.992124 -0.298944 -3.32913 0.237801 0.990745 -0.31403 -3.37593 0.242301 0.989298 -0.329327 -3.42273 0.246801 0.987781 -0.344835 -3.46953 0.251301 0.986194 -0.360553 -3.51632 0.255801 0.984535 -0.376481 -3.56311 0.260301 0.982805 -0.392621 -3.6099 0.264801 0.981001 -0.40897 -3.65669 0.269301 0.979124 -0.425531 -3.70347 0.273801 0.977171 -0.442302 -3.75026 0.278301 0.975143 -0.459283 -3.79705 0.282801 0.973037 -0.476475 -3.84383 0.287301 0.970854 -0.493877 -3.89062 0.291801 0.968592 -0.51149 -3.93741 0.296301 0.96625 -0.529314 -3.98421 0.300801 0.963828 -0.547348 -4.03101 0.305301 0.961323 -0.565593 -4.07782 0.309801 0.958737 -0.584048 -4.12464 0.314301 0.956067 -0.602715 -4.17146 0.318801 0.953312 -0.621591 -4.2183 0.323301 0.950472 -0.640679 -4.26515 0.327801 0.947545 -0.659978 -4.31201 0.332301 0.944532 -0.679487 -4.35889 0.336801 0.941429 -0.699208 -4.40577 0.341301 0.938238 -0.719139 -4.45268 0.345801 0.934957 -0.739282 -4.4996 0.350301 0.931584 -0.759635 -4.54654 0.354801 0.92812 -0.7802 -4.59349 0.359301 0.924562 -0.800977 -4.64046 0.3638 0.92091 -0.821964 -4.68745 0.3683 0.917164 -0.843164 -4.73446 0.3728 0.913321 -0.864574 -4.78149 0.3773 0.909382 -0.886197 -4.82853 0.3818 0.905345 -0.908031 -4.8756 0.3863 0.901209 -0.930077 -4.92268 0.3908 0.896974 -0.952335 -4.96978 0.3953 0.892638 -0.974805 -5.0169 0.3998 0.8882 -0.997487 -5.06404 0.4043 0.88366 -1.02038 -5.1112 0.4088 0.879017 -1.04349 -5.15837 0.4133 0.874268 -1.06681 -5.20556 0.4178 0.869415 -1.09034 -5.25277 0.4223 0.864455 -1.11408 -5.29999 0.4268 0.859388 -1.13804 -5.34723 0.4313 0.854212 -1.16221 -5.39448 0.4358 0.848927 -1.18659 -5.44175 0.4403 0.843532 -1.21118 -5.48903 0.4448 0.838026 -1.23599 -5.53632 0.4493 0.832408 -1.26101 -5.58363 0.4538 0.826677 -1.28624 -5.63095 0.4583 0.820831 -1.31169 -5.67827 0.4628 0.814871 -1.33735 -5.72561 0.4673 0.808795 -1.36322 -5.77295 0.4718 0.802602 -1.3893 -5.8203 0.4763 0.796291 -1.4156 -5.86766 0.4808 0.789861 -1.44211 -5.91502 0.4853 0.783311 -1.46883 -5.96238 0.4898 0.776641 -1.49577 -6.00975 0.4943 0.769849 -1.52292 -6.05713 0.4988 0.762934 -1.55029 -6.1045 0.5033 0.755896 -1.57786 -6.15188 0.5078 0.748733 -1.60565 -6.19925 0.5123 0.741445 -1.63366 -6.24663 0.5168 0.73403 -1.66187 -6.29401 0.5213 0.726487 -1.6903 -6.34139 0.5258 0.718817 -1.71894 -6.38877 0.5303 0.711016 -1.7478 -6.43615 0.5348 0.703086 -1.77687 -6.48353 0.5393 0.695024 -1.80615 -6.53091

  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

1 answer

3

The mistake "The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a.item(), a.any() or a.all()" occurs when python cannot make a comparison because the logical test is unclear. For example, the following replicable code also returns this error:

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                'B': [5, 6, 7, 8, 9],
                'C': ['a', 'b', 'c', 'd', 'e']})

if 3< df['A'].iloc[0:2]:
    print('ok')

Looking at your code, it seems that this error occurs in the expression elemento < y[0].iloc[i:i+1] since you are comparing a number with a list. python does not know which number to use in the comparison and returns the error.

  • I thought I was comparing it to the element that is in the y[0] list that is in the y[0] position. iloc[i:i+1]

  • 3

    No. Pq vc is slicing using ':' on a one-dimensional vector. Perhaps you have mistaken the operator ':' for ','. See this guide: https://medium.com/horadecodar/data-science-tips-02-como-usar-loc-e-iloc-no-pandas-fab58e214d87

  • Lucas I read here. I agree with what you said. It makes sense. You could tell me how to find the number of a list to do this comparison (number with number) ?

  • Lists are an example of sequential objects in python. Among other properties, sequential objects accept the subscript operator. This means that to access, for example, the third element of a list a = [1,2,3,4], just do a[2] (python starts counting from scratch). More details here (on Eng): https://en.wikibooks.org/wiki/Python_Programming/Sequences

  • Beauty. So turning the columns of the dataframe into a list solves my problem. I’ll do it now. I’ll pass you the feedback of the result. Thank you so much for sharing your time and knowledge with me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.