[英]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:我需要计算值 -9999 出现的次数并按年和月分组。所需的 output 类似于:
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'
Boolean select, groupby sum should give you what you need much easily Boolean select,groupby sum 应该可以轻松满足您的需求
if df['ghi mean']
is a float or integer如果
df['ghi mean']
是浮点数或 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']
字符串,则
(df['ghi mean']=='-9999.0').groupby(df['local time'].dt.strftime('%Y-%m')).sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.