Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count how many times a value appears per month in dataframe

I have the following time series with hourly data of several years:

    local time         ghi mean
0       2013-01-01 00:00:00 0.0
1       2013-01-01 01:00:00 0.0
2       2013-01-01 02:00:00 -9999
3       2013-01-01 03:00:00 0.0
4       2013-01-01 04:00:00 0.0
..         ...          ...
8754    2016-12-31 18:00:00 427.5
8755    2016-12-31 19:00:00 194.9
8756    2016-12-31 20:00:00 -9999
8757    2016-12-31 21:00:00 237.6
8758    2016-12-31 22:00:00 -9999
8759    2016-12-31 23:00:00 0.0

And I need to count how many times the value -9999 appears and group by year and month.The desired output would be something similar to:

    local time     ghi mean
0    2013-01   1
..         ...          ...
8    2016-12   2

I tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.groupby(df["local time"].dt.strftime('%Y-%m')).df['ghi mean'].value_counts()[-9999]

But got:

AttributeError: 'Series' object has no attribute 'df'

>Solution :

Boolean select, groupby sum should give you what you need much easily

if df['ghi mean'] is a float or integer

  (df['ghi mean']==-9999).groupby(df['local time'].dt.strftime('%Y-%m')).sum()

if you made df['ghi mean']a string then

(df['ghi mean']=='-9999.0').groupby(df['local time'].dt.strftime('%Y-%m')).sum()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading