grouping by day of the week pandas

前端 未结 3 1253
后悔当初
后悔当初 2021-01-28 17:10
  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-         


        
3条回答
  •  死守一世寂寞
    2021-01-28 17:46

    Wen's answer with value_counts is good, but does not account for the possibility of NaNs in the pct column.


    Assuming Index is the index, you can call 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.

提交回复
热议问题