python - 使用 Pandas 标记分组数据集中的最后一组行

标签 python pandas

编辑:以下问题给出 df.assign(Flag=1 - df.duplicate(['id', 'quarter', 'lot'], keep='last' )) 下面建议的方法

 index   mkid     ordernumber quarter lotnumber1 order_xldate  Flag
441670  10176228  0108595504  2015Q2    12947-1   2015-04-09     0
441211  10176228  0108663905  2015Q2    12947-1   2015-04-29     1
450008  10176228  0108663905  2015Q2     129161   2015-04-29     1
440268  10176228  0108779992  2015Q2    12987-1   2015-05-29     0
448187  10176228  0108779992  2015Q2    12848-1   2015-05-29     1
439085  10176228  0108895691  2015Q2    12987-1   2015-06-29     1
446123  10176228  0108895691  2015Q2    12965-1   2015-06-29     1
419419  10176228  0109003405  2015Q3    12969-1   2015-07-27     1
429893  10176228  0109003405  2015Q3    12987-1   2015-07-27     1
426850  10176228  0109241988  2015Q3      13929   2015-09-15     1
384762  10176228  0109385611  2015Q4     K10127   2015-10-09     1

Flag 字段应用于给定季度的每个 ordernumber而不是最后 订单号。因此,在上面,对于 2015Q2Flag 应该仅针对 ordernumber 0108895691

的两行命中 <小时/>

之前的帖子:

我目前有一个销售订单数据集,其中每个 ordernumber 分为批处理(即 lot1lot2 等) 。因此,每个订单号可能有不同的行。其他相关列包括帐户 idquarter(即 2018Q2)。对于每个给定季度中的每个给定id,我想使用 Pandas 对该给定季度内最后一个订单的所有批处理应用一个标志/Python。有什么建议吗?

目前有:

masterDF['FLAG'] = masterDF.groupby(by=['id','quarter'],as_index=False)['ordernumber'].nth(-1)
masterDF['LAST_ORDER_OF_QUARTER'] = np.where(masterDF['FLAG'].isnull(),0,1)

但是,如果该订单号出现在,则只会在该 id/quarter/order 组合的最后一行上放置 1,而不是在给定订单中的所有行上放置 1超过一行。

我想要的输出是两批 orderB 都为 1

id   |   quarter   |   ordernumber   |   lot      |    Last Order of Quarter
----------------------------------------------------------------------------
A    |   2018Q1    |   orderA        |   lot1     |     0
A    |   2018Q1    |   orderB        |   lot1     |     1
A    |   2018Q1    |   orderB        |   lot2     |     1

而不是:

id   |   quarter   |   ordernumber   |   lot      |    Last Order of Quarter
----------------------------------------------------------------------------
A    |   2018Q1    |   orderA        |   lot1     |     0
A    |   2018Q1    |   orderB        |   lot1     |     0
A    |   2018Q1    |   orderB        |   lot2     |     1

有什么建议吗?

最佳答案

使用重复

df.assign(Flag=1 - df.duplicated(['id', 'quarter', 'lot'], keep='last'))

  id quarter ordernumber   lot  Flag
0  A  2018Q1      orderA  lot1     0
1  A  2018Q1      orderB  lot1     1
2  A  2018Q1      orderB  lot2     1
<小时/>

同样的事情

df.assign(**{'Last Order': 1 - df.duplicated(['id', 'quarter', 'lot'], keep='last')})

  id quarter ordernumber   lot  Last Order
0  A  2018Q1      orderA  lot1           0
1  A  2018Q1      orderB  lot1           1
2  A  2018Q1      orderB  lot2           1

关于python - 使用 Pandas 标记分组数据集中的最后一组行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211840/

相关文章:

python - nlargest on groupby 具有多索引和多个聚合列

python - 动态向类添加属性

python - 迭代 DataFrame 行以创建新列,同时引用其他行

python - 单击 QTreeView 项目时防止 QComboboxView 自动折叠

python - get_text() 或文本属性不适用于标签

python - 来自 pandas 数据帧的散点图中的 Matplotlib 图例

python - 将列添加到同一 DataFrame 中的列的末尾

python - 如何更改 matplotlib 在绘制时间戳对象时使用的步长?

python - Pygame随机显示多个​​图像

python - 用第二个数据框中的值填充一个 pandas 数据框,其中一些行和列是共同的