Pandas Datetime AVERAGE

会有一股神秘感。 提交于 2020-06-08 13:17:44

问题


DataFrame where Date is datetime:

   Column   |       Date             
:-----------|----------------------:
    A       |   2018-08-05 17:06:01 
    A       |   2018-08-05 17:06:02 
    A       |   2018-08-05 17:06:03 
    B       |   2018-08-05 17:06:07 
    B       |   2018-08-05 17:06:09 
    B       |   2018-08-05 17:06:11 

Return Table is;

   Column   |       Date            
:-----------|----------------------:
    A       |   2018-08-05 17:06:02 
    B       |   2018-08-05 17:06:09 

回答1:


For your example.

Your data:

df = pd.DataFrame(data=[['A', '2018-08-05 17:06:01'],
                   ['A', '2018-08-05 17:06:02'],
                   ['A', '2018-08-05 17:06:03'],
                   ['B', '2018-08-05 17:06:07'],
                   ['B', '2018-08-05 17:06:09'],
                   ['B', '2018-08-05 17:06:11']],
            columns = ['column', 'date'])

Solution:

df.date = pd.to_datetime(df.date).values.astype(np.int64)

df = pd.DataFrame(pd.to_datetime(df.groupby('column').mean().date))

Output:

                      date
column                    
A      2018-08-05 17:06:02
B      2018-08-05 17:06:09

I hope it will be helpful.



来源:https://stackoverflow.com/questions/52007139/pandas-datetime-average

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!