3

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

1
  • So you expected out like beyond ? Commented Nov 3, 2017 at 0:46

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

df.apply(lambda x : x.notnull().sum())
Out[262]: 
all        50
(8, 10]     9
(-1, 2]    12
(4, 6]      9
(2, 4]     12
(6, 8]      8
dtype: int64

Count of elements in the column which are numeric and nonzero

df.mask(df==0).apply(lambda x : x.notnull().sum())
Out[263]: 
all        48
(8, 10]     9
(-1, 2]    10
(4, 6]      9
(2, 4]     12
(6, 8]      8
dtype: int64

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.