按星期几分组大熊猫

时间:2017-12-21 22:44:17

标签: python 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%

我希望通过计算列pct中的值来按每周每周发生一次分组。例如,我们现在有:

 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

然后生成的数据框应如下所示:

    Index        Count   
 Monday            3
 Tuesday           2
 Wednesday         2

我试过df2=df.groupby(pd.Grouper(freq='D')).size().sort_values(ascending=False) 但它没有按星期几分组,也没有转换为日期索引到单词

3 个答案:

答案 0 :(得分:2)

使用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

答案 1 :(得分:1)

Wen对value_counts的回答很好,但未考虑NaN列中pct s的可能性。

假设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

要按工作日排序,请转换为pd.Categorical,如图here所示。

答案 2 :(得分:0)

您可以使用:

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