首页 \ 问答 \ 为什么叫“ABA_problem”?(Why called “ABA_problem”?)

为什么叫“ABA_problem”?(Why called “ABA_problem”?)

今天我知道ABA问题。

http://en.wikipedia.org/wiki/ABA_problem

顺便说一下,我突然想知道为什么叫“ABA”问题? 缩写?


today I knew ABA problem.

http://en.wikipedia.org/wiki/ABA_problem

By the way, suddenly, i just like to know why called "ABA" problem? abbreviation?


原文:https://stackoverflow.com/questions/20836319
更新时间:2023-06-23 14:06

最满意答案

IIUC:您可以尝试使用.groupby项目编号,然后在dataframe .shift使用.groupby ,如下所示。

假设原始数据框如下所示:

   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

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

相关问答

更多