简体   繁体   English

如何根据 pandas dataframe 中的多列按元素分组并将每组的元素数量保存在另一列中?

[英]How can I group by elements based on multiple columns in pandas dataframe and save the number of elements of each group in another column?

I have a dataframe of the next form:我有下一个形式的 dataframe:

+--------+--------+--------+
|  Col1  |  Col2  |  Col3  |
+--------+--------+--------+
|  1     |   2    |    1   |
+--------+--------+--------+
|  1     |   2    |    1   |
+--------+--------+--------+
|  1     |   3    |    1   |
+--------+--------+--------+
|  2     |   4    |    1   |
+--------+--------+--------+ 

I want to get the next dataframe:我想得到下一个dataframe:

+--------+--------+--------+--------+
|  Col1  |  Col2  |  Col3  |  Count |
+--------+--------+--------+--------+
|  1     |   2    |    1   |   2    |
+--------+--------+--------+--------+
|  1     |   2    |    1   |   2    |
+--------+--------+--------+--------+
|  1     |   3    |    1   |   1    |
+--------+--------+--------+--------+
|  2     |   4    |    1   |   1    |
+--------+--------+--------+--------+

How can I get that dataframe?我怎样才能得到那个 dataframe? I am trying with the size() method after grouping by the dataframe, but that result is not what I would like.在按 dataframe 分组后,我尝试使用 size() 方法,但结果不是我想要的。 I would like to get another column in which the number of occurrences of the full row appears.我想得到另一列,其中出现整行的出现次数。

Use GroupBy.transform to get an equal length vector back, and assing it as new column:使用GroupBy.transform得到一个相等长度的向量,并将其作为新列:

df['Count'] = df.groupby(df.columns.tolist())['Col1'].transform('size')

Output Output

   Col1  Col2  Col3  Count
0     1     2     1      2
1     1     2     1      2
2     1     3     1      1
3     2     4     1      1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 在熊猫数据框中创建一个包含组中数字元素的列(groupby) - Creating a column in a pandas dataframe containing the number elements in the group (groupby) 根据 pandas 中 3 列中的重复元素创建组列 - Create a group column based on duplicated elements within 3 columns in pandas 按多列分组并在 pandas 中将 dict 元素的中值作为新列 - Group by multiple columns and get median of dict elements as a new column in pandas 按日期时间列将Pandas Dataframe的所有元素分组 - Group all elements of Pandas Dataframe by Datetime column Pandas数据框-基于组的每一列的总和 - Pandas dataframe - sum of each column based on group 熊猫:按列元素分组 - Pandas: Group By Elements of a Column 如何根据每个组具有 n 行数的特定列在 pandas 中分组? 如果可能,还要从原始 dataframe 中删除? - How to group by in pandas based on specific columns where each group has n number of rows? Also delete from the original dataframe IF POSSIBLE? 通过单个列对多个列进行分组— Pandas Dataframe - Group Multiple Columns by a Single Column — Pandas Dataframe 如何根据多列值对 pandas 数据框进行分组、计数和取消堆叠? - How to group , count, and unstack a pandas dataframe based on multiple columns values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM