Finding the mean and standard deviation of a timedelta object in pandas df

前端 未结 4 1542
太阳男子
太阳男子 2020-12-16 11:29

I would like to calculate the mean and standard deviation of a timedelta by bank from a dataframe with two columns shown

4条回答
  •  [愿得一人]
    2020-12-16 12:05

    No need to convert timedelta back and forth. Numpy and pandas can seamlessly do it for you with a faster run time. Using your dropped DataFrame:

    import numpy as np
    
    grouped = dropped.groupby('bank')['diff']
    
    mean = grouped.apply(lambda x: np.mean(x))
    std = grouped.apply(lambda x: np.std(x))
    

提交回复
热议问题