Python Pandas Group by date using datetime data

前端 未结 2 721
慢半拍i
慢半拍i 2020-12-01 01:44

I have a column Date_Time that I wish to groupby date time without creating a new column. Is this possible the current code I have does not work.



        
2条回答
  •  遥遥无期
    2020-12-01 01:51

    You can use groupby by dates of column Date_Time by dt.date:

    df = df.groupby([df['Date_Time'].dt.date]).mean()
    

    Sample:

    df = pd.DataFrame({'Date_Time': pd.date_range('10/1/2001 10:00:00', periods=3, freq='10H'),
                       'B':[4,5,6]})
    
    print (df)
       B           Date_Time
    0  4 2001-10-01 10:00:00
    1  5 2001-10-01 20:00:00
    2  6 2001-10-02 06:00:00
    
    print (df['Date_Time'].dt.date)
    0    2001-10-01
    1    2001-10-01
    2    2001-10-02
    Name: Date_Time, dtype: object
    
    df = df.groupby([df['Date_Time'].dt.date])['B'].mean()
    print(df)
    Date_Time
    2001-10-01    4.5
    2001-10-02    6.0
    Name: B, dtype: float64
    

    Another solution with resample:

    df = df.set_index('Date_Time').resample('D')['B'].mean()
    
    print(df)
    Date_Time
    2001-10-01    4.5
    2001-10-02    6.0
    Freq: D, Name: B, dtype: float64
    

提交回复
热议问题