简体   繁体   English

计算一个值每月在 dataframe 中出现的次数

[英]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.

相关问题 计算每组字符串中单词出现的次数 - Count how many times a word appears per group of strings 计算每个令牌出现在DataFrame的每一行中的次数 - Count how many times each token appears in each rows of a DataFrame 如何计算值在矩阵中出现的次数 - How to count how many times the value appears in the matrix 计算一个值在字典中出现多少次? (PYTHON 3) - Count how many times a value appears in dictionary ? (PYTHON 3) 计算一个 pandas dataframe 中的一对值出现在另一个中的次数 - Count how many times a pair of values in one pandas dataframe appears in another 计算一条记录在 pandas 数据框中出现的次数,并使用此计数器创建一个新功能 - count how many times a record appears in a pandas dataframe and create a new feature with this counter 在不使用 max 或 count 函数的情况下查找最大值并计算它在列表中出现的次数 - Finding the max value and count how many times it appears in a list without using max or count function 如何计算一个值在字典中出现的次数? - How to count the number of times a value appears in a dictionary? 计数出现一列的值的次数,并将列添加到数据框中 - Count times a value of a column appears and add a column to the dataframe with it 如何计算一个实体与另一个实体一起出现的次数 - How to count how many times an entity appears with another entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM