【问题标题】:Groupby two columns and comparison of rows of one column按两列分组并比较一列的行
【发布时间】:2021-06-13 18:43:57
【问题描述】:

我正在使用 groupby,但我不想丢失未包含在 groupby 中的其他列,例如我有一个 df:

id     date     name    item    price    unit    store
1    1/1/2020   abc    apples    200    Fruits   BigB
1    1/2/2020   abc    apples    100    Fruits   BigB
1    1/3/2020   abc    apples    250    Fruits   BigB
1    1/1/2020   abc    mangoes   350    Fruits   BigB
1    1/2/2020   abc    mangoes   150    Fruits   BigB
1    1/3/2020   abc    mangoes   50     Fruits   BigB
2    1/1/2020   xyz    apples    50     Fruits   BigB
2    1/2/2020   xyz    apples    50     Fruits   BigB

我想创建两列 flag 并基于 id 和 name 开始,如果 price 的值大于其前一行,则 flag 为 1 否则为 0。数据必须基于 id、name 和items.start 列基于标志。最初起始值将是其第一行的价格值。如果标志为 0,则开始具有先前的值,当标志更改为 1 时,开始也更改为其相应的价格值 输出将是:

id     date     name    item    price    unit    store  Flag      start
1    1/1/2020   abc    apples    200    Fruits   BigB   0          200
1    1/2/2020   abc    apples    100    Fruits   BigB   0          200
1    1/3/2020   abc    apples    250    Fruits   BigB   1          250
1    1/1/2020   abc    mangoes   350    Fruits   BigB   0          350 
1    1/2/2020   abc    mangoes   150    Fruits   BigB   0          350
1    1/3/2020   abc    mangoes   50     Fruits   BigB   0          350
2    1/1/2020   xyz    apples    50     Fruits   BigB   0          50
2    1/2/2020   xyz    apples    50     Fruits   BigB   0          50

Groupby 是按 id、name 和 item 列完成的。提前致谢。df 是按 id、name、item 和日期排序的。

【问题讨论】:

  • 第二个表没有grouped_by。比如你有三行 (id, name, item) = (1, abc, apples)
  • 其实我不想做实际的 groupby 我想在这三列的基础上添加一个 Flagh 列

标签: python pandas dataframe pandas-groupby


【解决方案1】:

方法

m = df[['id','name','item']].duplicated()

df['flag'] = df.eval('price > price.shift() and @m').astype(int)
df['start'] = df['price'].where(~m | df['flag']).ffill()

说明

考虑到列 idnameitem 识别数据框中的重复行以创建布尔掩码 m

>>> m

0    False
1     True
2     True
3    False
4     True
5     True
6    False
7     True
dtype: bool

在我们比较 price 列中的相邻行的框架上评估一个布尔表达式,并采用逻辑与掩码 m 以创建 flag

>>> df['flag']

0    0
1    0
2    1
3    0
4    0
5    0
6    0
7    0
Name: flag, dtype: int64

现在屏蔽price 列中不满足条件~m | df['flag'] 的值并向前填充以传播这些值

>>> df['start']

0    200.0
1    200.0
2    250.0
3    350.0
4    350.0
5    350.0
6     50.0
7     50.0
Name: start, dtype: float64

>>> df

   id      date name     item  price    unit store  start  flag
0   1  1/1/2020  abc   apples    200  Fruits  BigB  200.0     0
1   1  1/2/2020  abc   apples    100  Fruits  BigB  200.0     0
2   1  1/3/2020  abc   apples    250  Fruits  BigB  250.0     1
3   1  1/1/2020  abc  mangoes    350  Fruits  BigB  350.0     0
4   1  1/2/2020  abc  mangoes    150  Fruits  BigB  350.0     0
5   1  1/3/2020  abc  mangoes     50  Fruits  BigB  350.0     0
6   2  1/1/2020  xyz   apples     50  Fruits  BigB   50.0     0
7   2  1/2/2020  xyz   apples     50  Fruits  BigB   50.0     0

【讨论】:

  • 它给出了错误,说不能加入没有重叠的索引名称
  • @naina 我想这应该可以正常工作。 AFAIK 此错误与此处提出的解决方案无关。可能是您的代码的其他部分出现错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多