0

My goal: to know how many index satisfy criteria of 4<value<10 from a series object count_num.

I got the series object after df[].value_counts() like below (index of a,c,e satisfy the criteria)

index value

a 5

b 3

c 7

d 1

e 6

I tried but got error of cannot unpack non-iterable int object:

index_numb = []
for index, value in count_num:
    if 4<= value <= 10:
    index_numb += 1

Thank you very much for your time and comments

gboffi
  • 22,939
  • 8
  • 54
  • 85
Rookie
  • 7
  • 3
  • 1
    Try `index_numb = 0` – gboffi Sep 30 '22 at 16:22
  • In the question you wrote "4 < val < 10", in the code `4 <= val <= 10` - it's not the same... – gboffi Sep 30 '22 at 16:24
  • If you want to accumulate different answers, you probably need a list `my_list=[]`, using `my_list.append(new_answer)` to store the results, but if you just want to count you simply need a number, starting from zero: `my_count=0` , then `if condition_met : my_count+=1` — the answer you've accepted is not really a good answer from the point of view that I've just exposed. – gboffi Sep 30 '22 at 19:25

3 Answers3

1

You could do list comprehension to create a list of all the indicies that match and get the length of that, so something like len([index for index in count_num if 4 < count_num[index] < 10])

the len() part should be self-explanatory, just getting the length of whatever's passed in but the part inside is your list comprehension:

[index for index in count_num if 4 < count_num[index] < 10]

or basically create a list of all index values where count_num[index] is between 4 and 10.

That last conditional there are a few ways you can do it, either what I put, or if you didn't think that, count_num[index] < 4 and count_num[index] < 10 or even since it's a range do something like count-num[index] in range(4,10)

with the way you're doing it though you'll run into several errors.

First, the reason it's failing is because doing for index, value in count_num, the in part uses an iterator that's returning only the keys and so it's failing. you'd need to do for index, value in count_num.items()

After you fix that, your index_numb += 1 is indented wrong so it's not considered within the scope of your if statement so you'll get an error with that. After you fix that additionally, you're doing index_numb += 1 but index_numb is a list so adding 1 to it makes no sense. You probably meant to do index_numb = 0 then your index_numb += 1 will increment.

  • Thank you very much aznanimedude. I'm sorry I do not have enough reputation to cast the vote, so I would like to at least thank you in the comments. – Rookie Sep 30 '22 at 16:44
0

code:

index_numb = []

index_numb += 1

output :

Traceback (most recent call last):
  File "<string>", line 3, in <module>
TypeError: 'int' object is not iterable

other code:

index_numb = []

print(index_numb, type(index_numb))

index_numb += [1]

print(index_numb, type(index_numb))

output :

[] <class 'list'>
[1] <class 'list'>

see here better exolanation:

Why does += behave unexpectedly on lists? :

When dealing with lists like you are, though, the += operator is a shorthand for someListObject.extend(iterableObject). See the documentation of extend().

https://docs.python.org/3/tutorial/datastructures.html

pippo1980
  • 2,181
  • 3
  • 14
  • 30
0

I found a non-direct way to solve this problem. Expect some brief or direct methods to this!

df = series.to_frame() #convert the series object to df

df['screened_output'] = df['value'].apply(lambda x: 'true' if 4<= x <= 10 else 'false')     
index_4to10 = df['screened_output'].value_counts()

Then I can see the number of index satisfied or not, from the value of true and false...

Rookie
  • 7
  • 3
  • why didnt give us `df` and `count_num` too ? (is df panda dataframe ? ) so to test the entire code – pippo1980 Sep 30 '22 at 19:57
  • I gave the count_num in question, and df is dataframe. Thanks a lot for your suggestion. – Rookie Oct 01 '22 at 00:29
  • What type is count_num ? List of tuples , dictionary, pandas series ? Array ? – pippo1980 Oct 01 '22 at 07:58
  • series, as mentioned in the goal. Is the description of "series object" easy to confuse...? – Rookie Oct 02 '22 at 16:07
  • To me it is, my fault I am a novice, dont know how to create/instantiate a pandas.series, how to write it down to be able to reproduce it, how to read it. I can read, lists or dicts not series, sorry – pippo1980 Oct 02 '22 at 16:18
  • As said I did .value_counts() to a df then I got a pandas series. I think you are very good! I'm a real novice :) – Rookie Oct 02 '22 at 20:49
  • cant find them with alphanumeric index aa,b,c,... instead of 0,1,2,3 (https://www.w3schools.com/python/pandas/pandas_series.asp) – pippo1980 Oct 02 '22 at 21:19
  • Could you explain more? I don't understand what you mean ... Or anyway, this method did work for me. – Rookie Oct 03 '22 at 03:12
  • Ok i got it , that was the data frame ? So series is just 5,3,7,1,7 – pippo1980 Oct 03 '22 at 04:47