Pandas按标志ID分组,查找相对差异。

huangapple go评论109阅读模式
英文:

Pandas group by find the difference with respect to flag id's

问题

以下是翻译好的内容:

  1. 我有以下数据框:
  1. id flag col_1 col_2 name

0 1 1 11 13 a
1 2 0 62 14 b
2 1 0 13 15 a
3 2 1 74 16 b
4 3 1 25 17 c
5 3 0 22 18 c

  1. 我需要以下输出:
  1. id col_3 col_4 name

0 1 2 2 a
1 2 -12 -2 b
2 3 -3 1 c

  1. 我需要按idname分组,然后获取具有相同idnamecol_1中的flag[0]减去flag[1]。
  2. 提前感谢。
英文:

I Have the following Data frame:

  1. id flag col_1 col_2 name
  2. 0 1 1 11 13 a
  3. 1 2 0 62 14 b
  4. 2 1 0 13 15 a
  5. 3 2 1 74 16 b
  6. 4 3 1 25 17 c
  7. 5 3 0 22 18 c

I need this as the output -

  1. id col_3 col_4 name
  2. 0 1 2 2 a
  3. 1 2 -12 -2 b
  4. 2 3 -3 1 c

I need to group by id, name and take flag[0] of col_1 - flag[1] of the col_1 which has id, name in common.
Thanks in advance.

答案1

得分: 3

  1. # 使用简单的索引与临时索引(`set_index` 和 `reset_index`)
  2. tmp = df.set_index(['flag', 'id', 'name'])
  3. out = (tmp.loc[0] - tmp.loc[1]).reset_index()

输出:

  1. id name col_1 col_2
  2. 0 1 a 2 2
  3. 1 2 b -12 -2
  4. 2 3 c -3 1

使用的输入:

  1. df = pd.DataFrame({'id': [1, 2, 1, 2, 3, 3],
  2. 'flag': [1, 0, 0, 1, 1, 0],
  3. 'col_1': [11, 62, 13, 74, 25, 22],
  4. 'col_2': [13, 14, 15, 16, 17, 18],
  5. 'name': ['a', 'b', 'a', 'b', 'c', 'c']})
英文:

Using simple indexing with a temporary index (set_index and reset_index)

  1. tmp = df.set_index(['flag', 'id', 'name'])
  2. out = (tmp.loc[0] - tmp.loc[1]).reset_index()

Output:

  1. id name col_1 col_2
  2. 0 1 a 2 2
  3. 1 2 b -12 -2
  4. 2 3 c -3 1

Used input:

  1. df = pd.DataFrame({'id': [1, 2, 1, 2, 3, 3],
  2. 'flag': [1, 0, 0, 1, 1, 0],
  3. 'col_1': [11, 62, 13, 74, 25, 22],
  4. 'col_2': [13, 14, 15, 16, 17, 18],
  5. 'name': ['a', 'b', 'a', 'b', 'c', 'c']})

答案2

得分: 3

以下是翻译好的内容:

  1. 另一个可能的解决方案
  2. out = (
  3. df.groupby(["id", "name"])
  4. .apply(lambda g: -g.pop("flag").diff().max() * g.diff())
  5. .dropna().droplevel(2).reset_index()
  6. )
  7. 输出
  8. print(out)
  9. id name col_1 col_2
  10. 0 1 a 2.00 2.00
  11. 1 2 b -12.00 -2.00
  12. 2 3 c -3.00 1.00
英文:

Another possible solution :

  1. out = (
  2. df.groupby(["id", "name"])
  3. .apply(lambda g: -g.pop("flag").diff().max() * g.diff())
  4. .dropna().droplevel(2).reset_index()
  5. )

Output :

  1. print(out)
  2. id name col_1 col_2
  3. 0 1 a 2.00 2.00
  4. 1 2 b -12.00 -2.00
  5. 2 3 c -3.00 1.00

答案3

得分: 0

使用 DataFrame.pivot 进行重塑,可以通过 DataFrame.xs 选择 0/1 水平的 flag 列,并进行相减操作,然后通过 DataFrame.reset_index 将多重索引转换为列,最后通过 DataFrame.reindex 获取原始列的顺序(过滤掉 flag 列):

  1. df1 = df.pivot(index=['id','name'], columns='flag')
  2. out = (df1.xs(0, axis=1, level=1).sub(df1.xs(1, axis=1, level=1))
  3. .reset_index()
  4. .reindex(df.columns.difference(['flag'], sort=False), axis=1))
  5. print (out)
  6. id col_1 col_2 name
  7. 0 1 2 2 a
  8. 1 2 -12 -2 b
  9. 2 3 -3 1 c
英文:

Use DataFrame.pivot for reshape, so possible select 0/1 levels by flag column by DataFrame.xs and subtract, last convert Mulitindex in index to columns by DataFrame.reset_index and get original order of columns by DataFrame.reindex (with filter out flag column):

  1. df1 = df.pivot(index=['id','name'], columns='flag')
  2. out = (df1.xs(0, axis=1, level=1).sub(df1.xs(1, axis=1, level=1))
  3. .reset_index()
  4. .reindex(df.columns.difference(['flag'], sort=False), axis=1))
  5. print (out)
  6. id col_1 col_2 name
  7. 0 1 2 2 a
  8. 1 2 -12 -2 b
  9. 2 3 -3 1 c

答案4

得分: 0

另一种可能的解决方案:

  1. (df.groupby(['id', 'name'])[['flag', 'col_1', 'col_2']]
  2. .apply(lambda x: x.sort_values('flag', ascending=False).diff())
  3. .dropna().droplevel(2).drop('flag', axis=1).reset_index())

输出:

  1. id name col_1 col_2
  2. 0 1 a 2.0 2.0
  3. 1 2 b -12.0 -2.0
  4. 2 3 c -3.0 1.0
英文:

Another possible solution:

  1. (df.groupby(['id', 'name'])[['flag', 'col_1', 'col_2']]
  2. .apply(lambda x: x.sort_values('flag', ascending=False).diff())
  3. .dropna().droplevel(2).drop('flag', axis=1).reset_index())

Output:

  1. id name col_1 col_2
  2. 0 1 a 2.0 2.0
  3. 1 2 b -12.0 -2.0
  4. 2 3 c -3.0 1.0

huangapple
  • 本文由 发表于 2023年6月19日 15:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定