2

I am trying to create a for loop that iterates through a pandas Series, ageNew, that fills a list, label , based on the contents of the pandas Series.

This is the code that I have which outputs errors:

In:

for i in ageNew.items():
    if ageNew[i] <= 100:
        val = 0
    else:
        val = 1
    label.append(val)

Out:

KeyError: (0, 218)
cool_beans
  • 131
  • 1
  • 5
  • 15
  • `for k,v in age_new.items()` **don't use camalCase** – Druta Ruslan May 22 '18 at 15:31
  • 3
    You can use [`.values`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.values.html) instead of [`.items()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.items.html): `for i in ageNew.values:` – pault May 22 '18 at 15:33

3 Answers3

5

use vector operations instead of loops for efficiency and brevity

label = (age_new > 100).astype(int).tolist()
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
  • 2
    More performant version: `(age_new.values > 100).astype(int).tolist()` – jpp May 22 '18 at 15:37
  • oh that is cool, just out of curiosity, how would a vector operation work for a non-binary value, i.e. label can equal 0,1, or 2. Would it be like this: `((age_new <= 42) && (age_new > 7)).astype(int).tolist()` – cool_beans May 22 '18 at 15:49
  • 1
    almost! use the bit-wise `and` operator `&` instead of `&&`. `&&` is not a valid python operator. Similarly, the bit-wise `or` operator is `|` and not `||` – Haleemur Ali May 22 '18 at 16:01
1

when you use item() you need to pass two arguments in for statment example:

for k,v in dct.items():
    print(k, v)
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
0

You can use .values instead of .items() to get just the value:

for v in dct.values:
    if v <= 100:
        ...
fileyfood500
  • 1,199
  • 1
  • 13
  • 29