[英]How to count occurrences of each unique value within a column of lists Pandas
[英]How do I group by two columns and then count the occurrences of each unique value in a third column for each of the groupings?
我有一個唯一標識符,我想按 ["EMID"] 以及日期列 ["DateNew"] 對其進行分組。 然后我想計算 BRalpha 中每個值在每個分組中出現的次數。
數據集:
EMID | 日期新 | BRalpha |
---|---|---|
SIM10001 | 2016-06-01 | 腰椎 |
SIM10001 | 2016-06-01 | 腰椎 |
SIM10001 | 2016-07-01 | 腰椎 |
SIM10001 | 2016-07-01 | 雷神 |
SIM10002 | 2016-02-01 | NSPC |
SIM10002 | 2016-02-01 | NSPC |
SIM10002 | 2016-02-01 | NSPC |
SIM10002 | 2016-02-01 | NSPC |
SIM10002 | 2016-02-01 | NSPC |
SIM10003 | 2017-03-01 | ANFT |
SIM10003 | 2017-03-01 | ANFT |
所需的 output:
EMID | 日期新 | 計數_LUMB | Count_THOR | Count_NSPC | Count_ANFT |
---|---|---|---|---|---|
SIM10001 | 2016-06-01 | 2 | 0 | 0 | 0 |
SIM10001 | 2016-07-01 | 1 | 1 | 0 | 0 |
SIM10002 | 2016-02-01 | 0 | 0 | 5 | 0 |
SIM10003 | 2017-03-01 | 0 | 0 | 0 | 2 |
print(
df.groupby(["EMID", "DateNew", "BRalpha"])
.size()
.unstack()
.fillna(0)
.astype(int)
.add_prefix("count_")
.reset_index()
)
印刷:
BRalpha EMID DateNew count_ANFT count_LUMB count_NSPC count_THOR
0 SIM10001 2016-06-01 0 2 0 0
1 SIM10001 2016-07-01 0 1 0 1
2 SIM10002 2016-02-01 0 0 5 0
3 SIM10003 2017-03-01 2 0 0 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.