简体   繁体   English

pandas - 根据另一列中的每个唯一值计算DataFrame中值的出现次数

[英]pandas - Counting occurrences of a value in a DataFrame per each unique value in another column

Supposing that I have a DataFrame along the lines of: 假设我有一个DataFrame:

    term      score
0   this          0
1   that          1
2   the other     3
3   something     2
4   anything      1
5   the other     2
6   that          2
7   this          0
8   something     1

How would I go about counting up the instances in the score column by unique values in the term column? 如何通过term列中的唯一值来计算score列中的实例? Producing a result like: 产生如下结果:

    term      score 0     score 1     score 2     score 3
0   this            2           0           0           0
1   that            0           1           1           0
2   the other       0           0           1           1
3   something       0           1           1           0
4   anything        0           1           0           0

Related questions I've read here include Python Pandas counting and summing specific conditions and COUNTIF in pandas python over multiple columns with multiple conditions , but neither seems to quite be what I'm looking to do. 我在这里读到的相关问题包括Python Pandas计算和总结特定条件,以及pandas python中的COUNTIF在具有多个条件的多个列上 ,但似乎都不是我想要做的。 pivot_table as mentioned at this question seems like it could be relevant but I'm impeded by lack of experience and the brevity of the pandas documentation. 这个问题中提到的pivot_table似乎可能是相关的但是由于缺乏经验和熊猫文档的简洁性而受到阻碍。 Thanks for any suggestions. 谢谢你的任何建议。

Use groupby with size and reshape by unstack , last add_prefix : 使用groupbysize和重塑unstack ,最后add_prefix

df = df.groupby(['term','score']).size().unstack(fill_value=0).add_prefix('score ')

Or use crosstab : 或者使用crosstab

df = pd.crosstab(df['term'],df['score']).add_prefix('score ')

Or pivot_table : 或者pivot_table

df = (df.pivot_table(index='term',columns='score', aggfunc='size', fill_value=0)
        .add_prefix('score '))

print (df)
score      score 0  score 1  score 2  score 3
term                                         
anything         0        1        0        0
something        0        1        1        0
that             0        1        1        0
the other        0        0        1        1
this             2        0        0        0

You can also use, get_dummies , set_index , and sum with level parameter: 你也可以使用, get_dummiesset_index ,并sumlevel的参数:

(pd.get_dummies(df.set_index('term'), columns=['score'], prefix_sep=' ')
   .sum(level=0)
   .reset_index())

Output: 输出:

        term  score 0  score 1  score 2  score 3
0       this        2        0        0        0
1       that        0        1        1        0
2  the other        0        0        1        1
3  something        0        1        1        0
4   anything        0        1        0        0

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

相关问题 计算 pandas dataframe 中每个项目每月的出现次数 - Counting number of occurrences for each item per month in pandas dataframe Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 如何为每个唯一列值 pandas dataframe 添加行系列? - How to add row series per unique column value pandas dataframe? 确定熊猫数据框中每列的最大值 - determine column maximum value per another column in pandas dataframe Python Pandas:仅当列值唯一时,才将数据框追加到另一个数据框 - Python Pandas: Append Dataframe To Another Dataframe Only If Column Value is Unique 识别 Pandas DataFrame 列中连续出现的值 - Identifying consecutive occurrences of a value in a column of a pandas DataFrame 从数据帧的最后一行计算数据帧列中值的出现次数 - Counting occurrences of value in dataframe column from last row of dataframe 计算每个*唯一*列值的出现次数 - Calculate the number of occurrences of a column value value *per* unique id pandas groupby 和列的每个值出现的百分比 - pandas groupby and percentage of occurrences of each value of a column 熊猫计算列中每个值的出现次数 - Pandas count the occurrences of each value in column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM