[英]Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column?
[英]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?
我有下一個形式的 dataframe:
+--------+--------+--------+
| Col1 | Col2 | Col3 |
+--------+--------+--------+
| 1 | 2 | 1 |
+--------+--------+--------+
| 1 | 2 | 1 |
+--------+--------+--------+
| 1 | 3 | 1 |
+--------+--------+--------+
| 2 | 4 | 1 |
+--------+--------+--------+
我想得到下一個dataframe:
+--------+--------+--------+--------+
| Col1 | Col2 | Col3 | Count |
+--------+--------+--------+--------+
| 1 | 2 | 1 | 2 |
+--------+--------+--------+--------+
| 1 | 2 | 1 | 2 |
+--------+--------+--------+--------+
| 1 | 3 | 1 | 1 |
+--------+--------+--------+--------+
| 2 | 4 | 1 | 1 |
+--------+--------+--------+--------+
我怎樣才能得到那個 dataframe? 在按 dataframe 分組后,我嘗試使用 size() 方法,但結果不是我想要的。 我想得到另一列,其中出現整行的出現次數。
使用GroupBy.transform
得到一個相等長度的向量,並將其作為新列:
df['Count'] = df.groupby(df.columns.tolist())['Col1'].transform('size')
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.