在Python中对列平均值进行排序

时间:2017-03-28 18:41:28

标签: python sorting

大约一年前,我在R中编写了相同的函数。现在我想在Python中做同样的事情:Sort matrix column in R 我创建了矩阵:

7 3 2
3 1 6 
4 3 1 

接下来,我对所有列进行了升序排序:

3 1 1 
4 3 2
7 3 6 

现在我希望排序列按平均值递增。输出看起来像这样:

1 1 3 
3 2 4 
3 6 7 

我的所有代码:

import numpy as np
a = np.matrix([[7,3,2],[3,1,6],[4,3,1]])
a.sort(axis=0)
b = a.mean(0)
print(a)

1 个答案:

答案 0 :(得分:1)

诀窍是将array转变为matrix而不是>>> a[:,np.argsort(np.array(a).mean(0))] matrix([[1, 1, 3], [3, 2, 4], [3, 6, 7]])

>>> a[:,a.A.mean(0).argsort()]
matrix([[1, 1, 3],
        [3, 2, 4],
        [3, 6, 7]])

这也可以写成:

import numpy as np
a = np.matrix([[7,3,2],[3,1,6],[4,3,1]])
a.sort(axis=0)
print(a[:,np.argsort(np.array(a).mean(0))])

代码将是:

select @sql = 'select AM.AUTHNO, '
        select @sql = @sql + 'AST.DESCR, '
        select @sql = @sql + 'AM.MEMBID_DISP as ''MEMBID'', '
        select @sql = @sql + 'convert(varchar(25),isnull(AM.MEMBNAME,'''')) as member_name, '
        select @sql = @sql + 'isnull(MM.SEX,'''') as member_sex, '
        select @sql = @sql + 'isnull(MM.BIRTH,'''') as member_birth, '
        select @sql = @sql + 'convert(varchar(25),isnull(HC.HPNAME,'''')) as member_healthplan, '
        select @sql = @sql + 'convert(varchar(25),isnull(PM.lastname,'''') + isnull('', '' + PM.firstname,'''')) as provider_name, '
        select @sql = @sql + 'PM.PROVID_DISP as ''PROVID'', '
        select @sql = @sql + '''1'' as ''SORT_ORDER'', AM.MEMBID as ''MEMB_KEYID'', AM.AUTHPCP as ''PROV_KEYID'' '
        select @sql = @sql + ', AM.DBKEY as ''DBKEY'' '
        select @sql = @sql + ', (select DESCR from DB where [KEY] = AM.DBKEY) as ''IPA'' '
        select @sql = @sql + ', AM.AUTHDATE, AM.REQDATE '
        select @sql = @sql + 'from Z_AUTH_MASTERS AM, Z_AUTH_STATUS AST, Z_PROV_MASTERS PM, Z_MEMB_MASTERS MM, Z_HP_CONTRACTS HC '
        select @sql = @sql + 'where AM.DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + ') '  -- **** CUSTOM
        select @sql = @sql + 'and AM.DBKEY = PM.DBKEY ' 
        select @sql = @sql + 'and AM.AUTHPCP = PM.PROVID '
        select @sql = @sql + 'and AM.DBKEY = AST.DBKEY ' 
        select @sql = @sql + 'and AM.STATUS = AST.CODE '
        select @sql = @sql + 'and AM.DBKEY = MM.DBKEY ' 
        select @sql = @sql + 'and AM.MEMBID = MM.MEMBID '
        select @sql = @sql + 'and AM.DBKEY = HC.DBKEY ' 
        select @sql = @sql + 'and AM.HPCODE = HC.HPCODE '
        if @REFPROV <> '%'
            select @sql = @sql + 'and AM.AUTHPCP_DISP = ''' + @REFPROV + ''' '
        if @AUTHNO <> '%'
            select @sql = @sql + 'and AM.AUTHNO like ''' + @AUTHNO + ''' '
        if @STATUS <> '%'
            select @sql = @sql + 'and AM.STATUS = ''' + @STATUS + ''' '
        if @REQPROV_FIRST <> '%'
            select @sql = @sql + 'and PM.FIRSTNAME like ''' + @REQPROV_FIRST + ''' '
        if @REQPROV_LAST <> '%'
            select @sql = @sql + 'and PM.LASTNAME like ''' + @REQPROV_LAST + ''' '
        if @MEMB_FIRST <> '%'
            select @sql = @sql + 'and MM.FIRSTNM like ''' + @MEMB_FIRST + ''' '
        if @MEMB_LAST <> '%'
            select @sql = @sql + 'and MM.LASTNM like ''' + @MEMB_LAST + ''' '
        if @MEMB_ID <> '%'
            select @sql = @sql + 'and AM.MEMBID_DISP like ''' + @MEMB_ID + ''' '
        if @REQDATE_OP = 'E'
            select @sql = @sql + 'and AM.REQDATE = ''' + @REQDATE_BEGIN + ''' '
        if @REQDATE_OP = 'B'
            select @sql = @sql + 'and (AM.REQDATE >= ''' + @REQDATE_BEGIN + ''' and AM.REQDATE <= ''' + @REQDATE_END + ''') '
        if @AUTDATE_OP = 'E'
            select @sql = @sql + 'and AM.AUTHDATE = ''' + @AUTDATE_BEGIN + ''' '
        if @AUTDATE_OP = 'B'
            select @sql = @sql + 'and (AM.AUTHDATE >= ''' + @AUTDATE_BEGIN + ''' and AM.AUTHDATE <= ''' + @AUTDATE_END + ''') '
        if @EXPDATE_OP = 'E'
            select @sql = @sql + 'and AM.EXPRDATE = ''' + @EXPDATE_BEGIN + ''' '
        if @EXPDATE_OP = 'B'
            select @sql = @sql + 'and (AM.EXPRDATE >= ''' + @EXPDATE_BEGIN + ''' and AM.EXPRDATE <= ''' + @EXPDATE_END + ''') '
        if @FILTER <> '%'
            select @sql = @sql + 'and ' + @FILTER + ' '

        -- only show auths where the Authpcp or reqprov are providers that are linked to this user
        if @IS_SUPER=0
            begin
            select @sql = @sql + 'and ((AM.AUTHPCP in (SELECT PROVID from USER_PROVIDERS where USER_ID = ' + convert(varchar(10),@USER_ID) + ' and DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + '))) '
            select @sql = @sql + 'or (AM.REQPROV in (SELECT PROVID from USER_PROVIDERS where USER_ID = ' + convert(varchar(10),@USER_ID) + ' and DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + ')))) '
            end


        -- add sort code here
        select @sql = @sql + 'order by SORT_ORDER asc, '
        if @SORT_COLUMN = 1
            select @sql = @sql + 'AM.AUTHNO desc, '
        if @SORT_COLUMN = 2
            select @sql = @sql + 'AM.STATUS asc, '
        if @SORT_COLUMN = 3
            select @sql = @sql + 'AM.MEMBID_DISP asc, '
        if @SORT_COLUMN = 4
            select @sql = @sql + 'MM.LASTNM asc, '
        if @SORT_COLUMN = 5
            select @sql = @sql + 'MM.SEX asc, '
        if @SORT_COLUMN = 6
            select @sql = @sql + 'MM.BIRTH asc, '
        if @SORT_COLUMN = 7
            select @sql = @sql + 'HC.HPNAME asc, '
        if @SORT_COLUMN = 8
            select @sql = @sql + 'PM.LASTNAME asc, '
        if @SORT_COLUMN = 9
            select @sql = @sql + '''IPA'' asc, '

        -- trim off last ', '
        select @sql = left(@SQL,len(@SQL)-1)
        exec sp_executesql @sql