pandas groupby descirbe 函数加unique,diff 等

问答 1311号炼丹师
Lv1 居士
发布在 Pandas   1117   0
问答 1311号炼丹师   1117   0

1.如何在describe 函数中增加diff(range) 和unique 统计量,封装成函数。 para['uniCount'] 写的有问题 运行为空 para = dfpartable.groupby(['Par'],as_index=True)['Value'].describe(percentiles=[.1, .9],include='all')#.sort_values(by = ['Par'],ascending= True) para = para.reset_index() para['uniCount'] = dfpartable.groupby('Par', as_index=True).apply(lambda x: x.Value.nunique())## **paraStep['uniCount'] = dfpartable.groupby(['Par','Step'], as_index=True).nunique() para['Diff'] = dfpartable.groupby('Par')['Value'].transform(lambda x: x.diff())#df.shift()-df 后一个数减去前一个数 ## ** para['Diff'][0] = 0 ## ** #df_2['diff'] = df_2.sort_values(by='DATETIME').groupby('ID3')['DATETIME'].transform(lambda x: x.diff())

    para['range'] = para['max']-para['min']        
    para['Tag'] = "" #df['Tag'] = float('nan')
    para.loc[(para['range']/para['mean']<0.1) | (para['uniCount']<6),'Tag']= "3_Stable"     
    para.loc[(para['max']== para['min']),'Tag'] =  "2_Single"
    para.loc[(para['max']==0) & (para['min']==0),'Tag'] = "1_0"
版权声明:作者保留权利,不代表意本站立场。如需转载请联系本站以及作者。

参与讨论

回复《 pandas groupby descirbe 函数加unique,diff 等

EditorJs 编辑器

沙发,很寂寞~
反馈
to-top--btn