0

I want to first iterate through the item number and then for a particular item, I need to iterate through the week number. Main aim is to flag the week number of a each item in which there is inventory addition. I am newbie, so I am not aware of how to do double iteration. Data

Output will look like this

Output will be like this. The flag becomes one when the inventory for the current week is greater than the previous one.

11
  • do you need a for loop inside a for loop? Commented May 18, 2018 at 13:14
  • What have you tried so far? Give us anything ..... Commented May 18, 2018 at 13:15
  • Are you using pandas? Or is that a list of lists Commented May 18, 2018 at 13:17
  • How is your data imported? Please note that an image is not as helpful as the real Text structure combined with the code snippet you usw for importing to understand what your situation is Commented May 18, 2018 at 13:18
  • 1
    @SKSingh if you can give us how your data is presented, we can help you Commented May 18, 2018 at 13:20

1 Answer 1

1

IIUC: You can try using .groupby item number followed by .shift in dataframe as following.

Suppose the original dataframe is as below:

   DIV_NBR  itm_nbr  WEEK_NO  DISTINCT_ITM_CNT  INVENTORY_IN_HAND
0       18    65874   201511               5.0             2925.0
1       18    65874   201512               5.0             2910.0
2       18    65874   201513               5.0             2961.0
3       19    65875   201511               5.0             2965.0
4       19    65875   201512               5.0             2971.0

Then:

# keep record of last week by grouping by item number and then using shift
df['LAST_WEEK'] = df.groupby('itm_nbr')['INVENTORY_IN_HAND'].shift()

# check if current inventory is greater than last week
df['Flag'] = (df['INVENTORY_IN_HAND'] - df['LAST_WEEK'])>0

# delete additional column
del df['LAST_WEEK']

# change flag int
df['Flag'] = df['Flag'].astype(int)

print(df)

Result:

   DIV_NBR  itm_nbr  WEEK_NO  DISTINCT_ITM_CNT  INVENTORY_IN_HAND  Flag
0       18    65874   201511               5.0             2925.0     0
1       18    65874   201512               5.0             2910.0     0
2       18    65874   201513               5.0             2961.0     1
3       19    65875   201511               5.0             2965.0     0
4       19    65875   201512               5.0             2971.0     1
Sign up to request clarification or add additional context in comments.

2 Comments

This is more or less exactly the way I'd describe, too. But imho instead of shift() and manually substracting just using diff() which does the same would be even more straight forward here.
I am not sure but I think someone may come up with efficient way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.