为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

MMMHUHU 2021-06-10 18:17:11
假设我有一个 DataFrame 如下:    term      score0   this          01   that          12   the other     33   something     24   anything      15   the other     26   that          27   this          08   something     1我将如何通过score列中的唯一值计算列中的实例term?产生如下结果:    term      score 0     score 1     score 2     score 30   this            2           0           0           01   that            0           1           1           02   the other       0           0           1           13   something       0           1           1           04   anything        0           1           0           0我在这里读到的相关问题包括Python Pandas 计数和求和特定条件和Pandas python 中的 COUNTIF 在多个具有多个条件的列上,但似乎都不是我想要做的。pivot_table正如在这个问题中提到的,它似乎可能是相关的,但由于缺乏经验和熊猫文档的简洁性,我受到了阻碍。感谢您的任何建议。
查看完整描述

2 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

使用groupbywithsize和 reshape by unstack, last add_prefix:


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

或使用crosstab:


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

或者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


查看完整回答
反对 回复 2021-06-22
?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

您还可以使用, get_dummies, set_index, 和sum带level参数:


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

   .sum(level=0)

   .reset_index())

输出:


        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


查看完整回答
反对 回复 2021-06-22
  • 2 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号