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:
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 1:
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()