Summary statistics for each group and transpose using pandas

青春壹個敷衍的年華 提交于 2021-01-01 09:03:14

问题


I have a dataframe like as shown below

df = pd.DataFrame({'person_id': [11,11,11,11,11,11,11,11,12,12,12],
                   'time' :[0,0,0,1,2,3,4,4,0,0,1],
                   'value':[101,102,np.nan,120,143,153,160,170,96,97,99]})

What I would like to do is

a) Get the summary statistics for each subject for each time point (ex: 0hr, 1hr, 2hr etc)

b) Please note that NA rows shouldn't be counted as separate record/row during computing mean

I was trying the below

for i in df['subject_id'].unique()
        df[df['subject_id'].isin([i])].time.unique
        val_mean = df.groupby(['subject_id','time']][value].mean()
        val_stddev = df[value].std()

But I couldn't get the expected output

I expect my output to be like as shown below where I expect one row for each time point (ex: 0hr, 1 hr , 2hr, 3hr etc). Please note that NA rows shouldn't be counted as seperated record/row during computing mean

来源:https://stackoverflow.com/questions/65262151/summary-statistics-for-each-group-and-transpose-using-pandas

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