Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count elements in pandas dataframe columns

Any suggestions for elegant ways to make the following counts for each column in my dataframe:

  • Count of elements in the column which are numeric (any value)
  • Count of elements in the column which are numeric and nonzero

Here's some code to generate a dataframe like mine (but much smaller):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#from matplotlib import style
import seaborn as sns

df = pd.DataFrame(data=np.random.random_integers(low=0,high=10,size=(50,1)),
                  columns=['all']
                  )

# set up the grouping
g = df.groupby(pd.cut(df['all'],[-1,2,4,6,8,10]))
for name, data in g.groups.items():
    df[name] = df.loc[data]['all']

This produces a dataframe that looks like the following:

dataframe

like image 429
Emily Beth Avatar asked Jul 08 '26 22:07

Emily Beth


1 Answers

You can do this without the apply:

In [11]: df.notnull().sum()
Out[11]:
all        50
(-1, 2]    12
(2, 4]     12
(4, 6]      7
(6, 8]     10
(8, 10]     9
dtype: int64

In [12]: (df.notnull() & (df != 0)).sum()
Out[12]:
all        46
(-1, 2]     8
(2, 4]     12
(4, 6]      7
(6, 8]     10
(8, 10]     9
dtype: int64

Note: This works as counting True values is the same as taking their sum.

like image 103
Andy Hayden Avatar answered Jul 11 '26 12:07

Andy Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!