[英]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.