在python中的数据帧的每列中计数非零值 [英] Counting non zero values in each column of a dataframe in python

查看:1280
本文介绍了在python中的数据帧的每列中计数非零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python-pandas数据框,其中第一列是user_id,其余的列是标签(tag_0到tag_122)。
我有以下格式的数据:

  UserId Tag_0 Tag_1 
7867688 0 5
7867688 0 3
7867688 3 0
7867688 3.5 3.5
7867688 4 4
7867688 3.5 0

我的目的是为每个user_id

$ b实现 Sum(Tag)/ Count(NonZero(Tags))
$ b

df.groupby('user_id')。sum(),给我 sum(tag),但是我无法计算非零值



是否可以实现 Sum(Tag)/ Count(NonZero ))在一个命令中



在MySQL中我可以实现如下: -

 从表组中选择user_id,sum(tag)/ count(nullif(tag,0))1 

任何帮助不胜感激。

解决方案

每一行中的非数组是

  df.astype(bool).sum(axis = 1)


I have a python-pandas-dataframe in which first column is user_id and rest of the columns are tags(tag_0 to tag_122). I have the data in the following format:

UserId  Tag_0   Tag_1
7867688 0   5
7867688 0   3
7867688 3   0
7867688 3.5 3.5
7867688 4   4
7867688 3.5 0

My aim is to achieve Sum(Tag)/Count(NonZero(Tags)) for each user_id

df.groupby('user_id').sum(), gives me sum(tag), however I am clueless about counting non zero values

Is it possible to achieve Sum(Tag)/Count(NonZero(Tags)) in one command?

In MySQL I could achieve this as follows:-

select user_id, sum(tag)/count(nullif(tag,0)) from table group by 1

Any help shall be appreciated.

解决方案

My favorite way of getting number of nonzeros in each row is

df.astype(bool).sum(axis=1)

这篇关于在python中的数据帧的每列中计数非零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆