Counting non zero values in each column of a dataframe in python

前端 未结 3 1140
温柔的废话
温柔的废话 2020-12-07 22:54

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:



        
3条回答
  •  时光说笑
    2020-12-07 22:59

    Why not use np.count_nonzero?

    1. To count the number of non-zeros of an entire dataframe, np.count_nonzero(df)
    2. To count the number of non-zeros of all rows np.count_nonzero(df, axis=0)
    3. To count the number of non-zeros of all columns np.count_nonzero(df, axis=1)

    It works with dates too.

提交回复
热议问题