如何按计数值分组(python,pandas)

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

How to group by counted value (python, pandas)

问题

你好,你可以尝试使用以下代码来实现你的需求:

  1. import pandas as pd
  2. # 创建DataFrame
  3. data = {'SerialNr': [1, 2, 3, 4],
  4. 'Amount': [2, 4, 3, 2]}
  5. df = pd.DataFrame(data)
  6. # 使用groupby和count函数来计算数量
  7. result = df.groupby('Amount')['SerialNr'].count().reset_index()
  8. result.columns = ['Amount', 'Quantity of SerialNr']
  9. # 打印结果
  10. print(result)

这段代码会根据 "Amount" 列进行分组,然后计算每个分组中的 "SerialNr" 数量,最后得到你所需的结果表格。

英文:

Hello I have this Dataframe:

SerialNr Amount
1 2
2 4
3 3
4 2

And I want a table that shows me how many serial numbers in the right column have the same numbers.
Here for example it would look like this

Amount Quantity of SerialNr
1 0
2 2
3 1
4 1

It should sum up the Serialnummer with the same amount

Tried to do it with groupby but did not work

答案1

得分: 1

根据您的期望输出需要Amount的数量,因此可以使用Series.value_counts结合Series.reindex,以1为起始点进行如下操作:

  1. s = df['Amount'].value_counts()
  2. df = (s.reindex(range(1, s.index.max()+1), fill_value=0)
  3. .rename_axis('Amount')
  4. .reset_index(name='Amount的数量'))
  5. print (df)
  6. Amount Amount的数量
  7. 0 1 0
  8. 1 2 2
  9. 2 3 1
  10. 3 4 1

或者使用Categorical

  1. cats = range(1, df.Amount.max()+1)
  2. df = (pd.Categorical(df['Amount'], categories=cats)
  3. .value_counts()
  4. .rename_axis('Amount')
  5. .reset_index(name='Amount的数量'))
  6. print (df)
  7. Amount Amount的数量
  8. 0 1 0
  9. 1 2 2
  10. 2 3 1
  11. 3 4 1
英文:

From your expected ouput need Quantity of Amount, so use Series.value_counts with Series.reindex by range starting by 1:

  1. s = df['Amount'].value_counts()
  2. df = (s.reindex(range(1, s.index.max()+1), fill_value=0)
  3. .rename_axis('Amount')
  4. .reset_index(name='Quantity of Amount'))
  5. print (df)
  6. Amount Quantity of SerialNr
  7. 0 1 0
  8. 1 2 2
  9. 2 3 1
  10. 3 4 1

Or use Categorical:

  1. cats = range(1, df.Amount.max()+1)
  2. df = (pd.Categorical(df['Amount'], categories=cats)
  3. .value_counts()
  4. .rename_axis('Amount')
  5. .reset_index(name='Quantity of Amount'))
  6. print (df)
  7. Amount Quantity of Amount
  8. 0 1 0
  9. 1 2 2
  10. 2 3 1
  11. 3 4 1

答案2

得分: 1

另一个可能的解决方案:

  1. (pd.concat([df.set_index('SerialNr'),
  2. df['Amount'].value_counts().rename('Amount的数量')], axis=1)
  3. .fillna(0, downcast='infer').reset_index()
  4. .drop('Amount', axis=1).rename({'index': 'Amount'}, axis=1))

输出:

  1. Amount count
  2. 0 1 0
  3. 1 2 2
  4. 2 3 1
  5. 3 4 1
英文:

Another possible solution:

  1. (pd.concat([df.set_index('SerialNr'),
  2. df['Amount'].value_counts().rename('Quantity of Amount')], axis=1)
  3. .fillna(0, downcast='infer').reset_index()
  4. .drop('Amount', axis=1).rename({'index': 'Amount'}, axis=1))

Output:

  1. Amount count
  2. 0 1 0
  3. 1 2 2
  4. 2 3 1
  5. 3 4 1

huangapple
  • 本文由 发表于 2023年6月26日 17:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555465.html
匿名

发表评论

匿名网友

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

确定