将相同的行在数据框中放在一起。

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

To put the same rows together in a data frame

问题

Sure, here's the translated code portion:

  1. 有一个包含三列的数据框型号model)、批次lot和数量qty
  2. import pandas as pd
  3. df = pd.DataFrame({'model':['A','A','A','B','B','B','C','C','C','C'],
  4. 'lot':['x','x','y','y','y','y','y','y','z','z'],
  5. 'qty':[1,2,1,3,1,4,3,2,2,2]})

I hope this helps!

英文:

There is one data frame with three columns: model, lot, and qty

  1. import pandas as pd
  2. df = pd.DataFrame({'model':['A','A','A','B', 'B','B','C','C','C','C'],
  3. 'lot':['x','x','y','y', 'y','y','y','y','z','z'],
  4. 'qty':[1,2,1,3,1,4,3,2,2,2]})

将相同的行在数据框中放在一起。

I want to put the same row together like below

Is it possible?

将相同的行在数据框中放在一起。

答案1

得分: 3

你可以使用:

  1. df.groupby(['model', 'lot'], as_index=False)['qty'].sum()
  2. model lot qty
  3. 0 A x 3
  4. 1 A y 1
  5. 2 B y 8
  6. 3 C y 5
  7. 4 C z 4
英文:

You can use:

  1. >>> df.groupby(['model', 'lot'], as_index=False)['qty'].sum()
  2. model lot qty
  3. 0 A x 3
  4. 1 A y 1
  5. 2 B y 8
  6. 3 C y 5
  7. 4 C z 4

答案2

得分: 1

以下是已翻译的内容:

应该可以与以下代码一起使用。

grouped = df.groupby(['model', 'lot'])['qty'].sum().reset_index()
grouped

英文:

It should hopefully work with the following code.

  1. grouped = df.groupby(['model', 'lot'])['qty'].sum().reset_index()
  2. grouped

huangapple
  • 本文由 发表于 2023年5月11日 15:46:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225222.html
匿名

发表评论

匿名网友

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

确定