Pandas Group Counting Frequency of NaT and Datevalues by month, week, day in the same colum

Viewed 32

So here is some biz logic to understand the problem:


In ['Stuffed'] there are dates, and NaT/(blanks).

  • if row has date = container is stuffed
  • if row is blank = container is not stuffed

I was able to get the Stuffed Containers per /month /week, /day
but Not the blanks!

Im trying to create a status of stuffed vs unstuffed /month , /week, /day

Any ideas?

# Containers database has multiple entries of the same ID so .nunique() is required to filter 

stuff_cont = df.groupby([pd.Grouper(key='Stuffed', freq='M')])['Container'].nunique() 

stuff_cont.head(10)


Stuffed
2019-01-31    179
2019-02-28    287
2019-03-31    462
2019-04-30    588
2019-05-31    587
2019-06-30    530
2019-07-31    317
2019-08-31    237
2019-09-30    163
2019-10-31    283
Name: Container, dtype: int64
1 Answers

Let's use pd.concat:

pd.concat([stuff_cont, df.loc[df['Stuffed'].isna(), 'Container'].count()])

This will combine the results with groupby and a count all all the NaN 'Stuffed' records.

Related