0

I have the following loop:

max_potential = 100
for indx, values in geography.sort_values(district)[neighborhood].items():
    population = geography_clusters[geography_clusters['neighborhood']==indx]['potential'].astype(int)
    potential_neighborhood = potential_neighborhood + population 
    if potential_neighborhood < max_potential:
        list_close_neighborhood.append(indx)

And I get the following error

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

on this part of the code:

if potential_neighborhood < max_potential:

I believe that as the input is a series, I can't make a conditional based on the whole series. What I want to do is mainly save the value of each neighborhood (that works as an index in the loop) if that row satisfies the following condition:

if potential_neighborhood < max_potential:

And append the [neighborhood] value on a list [list_close_neighborhood]. Is it possible?

SmackCat
  • 41
  • 4
  • 3
    You are comparing multiple numbers, a`pd.Series`, to a single element `max_potential`. Since the series contains multiple numbers, you need to specify if you want to have ALL or ANY of those numbers to be smaller than `max_potential`. What is it that you want to accomplish? – alec_djinn Jul 10 '23 at 20:14
  • What do you mean by a subset? Please make a [mre] including some example input(s), desired output, and complete but minimal code. For specifics see [How to make good reproducible pandas examples](/q/20109391/4518341). You are using Pandas, right? Please add the tag for it or whatever library you're using. For more tips, like how to write a good title, see [ask]. – wjandrea Jul 10 '23 at 20:21
  • Hi @wjandrea. What I want to do is mainly be able to store a value based on [potential_neighborhood] (a value calculated in every iteration) and append it to a list. That's what I meant by "subset". Sorry, english is not my native language – SmackCat Jul 10 '23 at 23:25

1 Answers1

0

Here’s a link to a popular question that should help you. It’s still out of my grasp but I get what you’re trying to do and I think the answers here will help you fix it without over complicating. Please let me know if I’m wrong!

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

LaurenKai
  • 38
  • 5
  • Hi @LaurenKai. What I want to do is mainly be able to store a value based on [potential_neighborhood] (a value calculated in every iteration) and append it to a list. That's what I meant by "subset". Sorry, english is not my native language. Do you think it is possible to achieve? – SmackCat Jul 10 '23 at 23:26