我想首先遍历项目编号,然后对于特定项目,我需要遍历周数。主要目的是标记每个项目的周数,其中有库存添加。我是新手,所以我不知道如何进行双重迭代。
输出将是这样的。当前周的库存大于前一个库存时,该标志变为1。
答案 0 :(得分:1)
IIUC:您可以尝试使用.groupby
项目编号,然后使用.shift
中的dataframe
,如下所示。
假设原始数据帧如下:
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
然后:
# 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)
结果:
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