简体   繁体   English

如何按两列分组,然后计算每个分组在第三列中每个唯一值的出现次数?

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

I have a unique identifier that I want to group by ["EMID"] along with a date column ["DateNew"].我有一个唯一标识符,我想按 ["EMID"] 以及日期列 ["DateNew"] 对其进行分组。 Then I would like to count the number of times each value in BRalpha occurs for each grouping.然后我想计算 BRalpha 中每个值在每个分组中出现的次数。

Data Set:数据集:

EMID EMID DateNew日期新 BRalpha BRalpha
SIM10001 SIM10001 2016-06-01 2016-06-01 LUMB腰椎
SIM10001 SIM10001 2016-06-01 2016-06-01 LUMB腰椎
SIM10001 SIM10001 2016-07-01 2016-07-01 LUMB腰椎
SIM10001 SIM10001 2016-07-01 2016-07-01 THOR雷神
SIM10002 SIM10002 2016-02-01 2016-02-01 NSPC NSPC
SIM10002 SIM10002 2016-02-01 2016-02-01 NSPC NSPC
SIM10002 SIM10002 2016-02-01 2016-02-01 NSPC NSPC
SIM10002 SIM10002 2016-02-01 2016-02-01 NSPC NSPC
SIM10002 SIM10002 2016-02-01 2016-02-01 NSPC NSPC
SIM10003 SIM10003 2017-03-01 2017-03-01 ANFT ANFT
SIM10003 SIM10003 2017-03-01 2017-03-01 ANFT ANFT

Desired output:所需的 output:

EMID EMID DateNew日期新 Count_LUMB计数_LUMB Count_THOR Count_THOR Count_NSPC Count_NSPC Count_ANFT Count_ANFT
SIM10001 SIM10001 2016-06-01 2016-06-01 2 2 0 0 0 0 0 0
SIM10001 SIM10001 2016-07-01 2016-07-01 1 1 1 1 0 0 0 0
SIM10002 SIM10002 2016-02-01 2016-02-01 0 0 0 0 5 5 0 0
SIM10003 SIM10003 2017-03-01 2017-03-01 0 0 0 0 0 0 2 2
print(
    df.groupby(["EMID", "DateNew", "BRalpha"])
    .size()
    .unstack()
    .fillna(0)
    .astype(int)
    .add_prefix("count_")
    .reset_index()
)

Prints:印刷:

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.

相关问题 如何计算一列列表中每个唯一值的出现次数 Pandas - How to count occurrences of each unique value within a column of lists Pandas Pandas 按两列分组,并按每组计算第二列值 - Pandas group by two columns and count the second column value by each group 按两列分组并计算 Pandas 中每个组合的出现次数 - Group by two columns and count the occurrences of each combination in Pandas Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers Python dataframe:: 为任一列中的每个唯一值获取跨两列的计数 - Python dataframe:: get count across two columns for each unique value in either column 熊猫计算列中每个值的出现次数 - Pandas count the occurrences of each value in column pandas:计算列表中每个元素在列表列中唯一出现的次数 - pandas: count the number of unique occurrences of each element of list in a column of lists 如何计算包含列表的字典中每个键的每个唯一值? - How do I count each unique value for each key in a dictionary containing lists? 如何获取 pandas 中每对唯一列的列值计数? - How to get count of column values for each unique pair of columns in pandas? 如何对 Pandas 中的列中的每个唯一值进行排序和计数 - How to sort and count each unique value in a column in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM