简体   繁体   English

按星期几分组熊猫

[英]grouping by day of the week pandas

  I have a dataframe,df 

        Index       eventName Count   pct
     2017-08-09       ABC     24     95.00%
     2017-08-09       CDE    140     98.50%
     2017-08-10       DEF    200     50.00%
     2017-08-11       CDE    150     99.30%
     2017-08-11       CDE    150     99.30%
     2017-08-16       DEF    200     50.00%
     2017-08-17       DEF    200     50.00%

I want to group by daily weekly occurrence by counting the values in the column pct. 我想通过对列pct中的值进行计数来按每日每周发生次数进行分组。 for example, we now have: 例如,我们现在有:

 2017-08-09 has 2 values in pct column  and  2017-08-16 has 1 value in pct, then we have Monday:3 
  2017-08-10  has 1 value and 2017-08-17 has 1 value,then we have Tuesday:2 and so on

then the resulting dataframe should look like this: 那么结果数据框应如下所示:

    Index        Count   
 Monday            3
 Tuesday           2
 Wednesday         2

I have tried df2=df.groupby(pd.Grouper(freq='D')).size().sort_values(ascending=False) but its not grouping by day of the week and not transforming to the date index to words 我试过了df2=df.groupby(pd.Grouper(freq='D')).size().sort_values(ascending=False)但它不是按星期几分组,也不会转换为单词的日期索引

By using value_counts 通过使用value_counts

df.Index=pd.to_datetime(df.Index)
df.Index.dt.weekday_name.value_counts()
Out[994]: 
Wednesday    3
Thursday     2
Friday       2
Name: Index, dtype: int64

Wen's answer with value_counts is good, but does not account for the possibility of NaN s in the pct column. Wen对value_counts的回答是好的,但没有考虑pct列中NaN的可能性。


Assuming Index is the index, you can call groupby + count - 假设Index是指数,你可以调用groupby + count -

df.index = pd.to_datetime(df.index)
df.groupby(df.index.weekday_name).pct.count()

Index
Friday       2
Thursday     2
Wednesday    3
Name: pct, dtype: int64

To sort on weekday, convert to pd.Categorical , as shown here . 要排序平日,转换为pd.Categorical ,如图所示这里

You can use: 您可以使用:

df.rename(columns={'Index': 'New_name'}, inplace=True)

df['New_name'] = pd.to_datetime(df['New_name'])

df['Day_df'] = df['New_name'].dt.weekday_name

df.groupby(['Day_df']).count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM