-1

I am running the below code, but python is giving some weird error which it was not giving before:

 for i in range(len(df)):
    if df['event_name'][i] == 'subcategory_click':
        df.at[i,'subcategory_navigation']=1

Input:

user_id event_name
10     psuedo_App_start
10     subcategory_click
10     subcategory_click
10     subcategory_click
10     product_search
10     product_search
10     product_search

Desired Output:

user_id event_name  subcategory_navigation
10     psuedo_App_start     0
10     subcategory_click    1
10     subcategory_click    1
10     subcategory_click    1
10     product_search       0
10     product_search       0
10     product_search       0

PFA the error image: enter image description here

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
nk23
  • 179
  • 1
  • 10
  • 4
    Possible duplicate of [I'm getting Key error in python](https://stackoverflow.com/questions/10116518/im-getting-key-error-in-python) – mx0 Apr 07 '19 at 16:44
  • `print(repr(df))` to see what is in there. `KeyError` indicates that either `'event_name'` doesn't exist in `df`, or `i` doesn't exist in `df['event_name']`. Do `print(df['event_name'])` to figure out which one it is. – pts Apr 07 '19 at 17:06
  • You are iterating over the "indices" of `df`, not `df['event_name']`. If you didn't have that problem before, it was a coincidence. – chepner Apr 07 '19 at 17:12
  • Use `for i in range(len(df['event_name']))`, or better `for i, ev in enumerate(df['event_name']): if ev == 'product_search': a = i`. – chepner Apr 07 '19 at 17:14

4 Answers4

0

This error seems to be saying that df does not contain a key named 'event_name'. The error could also be due to i not existing in df['event_name'], but it appears that you'd expect that collection to be a list, not a map.

I see a potential problem in your logic. You're iterating based on the length of df, but then you're referencing a different list in your loop, df['event_name']. I don't see where the lengths of these two lists should be related. Do you maybe want for i in range(len(df['event_name']) ? - from your code, the length of df does not seem to be a valuable quantity, as it is a map, and you're referring that map's keys explicitly.

It's hard to know what's going on because you don't show us what df is. I'd suggest that you run the code in a debugger, stop on that line, and see what df contains at that point.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0
df["subcategory_navigation"] = (df["event_name"] == "subcategory_click").astype(int)

should do the job

Lante Dellarovere
  • 1,838
  • 2
  • 7
  • 10
0

Try switching ['event_name'] and [i] in the if statement

jtlz2
  • 7,700
  • 9
  • 64
  • 114
0

df.reset_index(inplace=True) .. I used this before the for loop, somehow it worked

nk23
  • 179
  • 1
  • 10