{"id":1560,"date":"2021-05-14T03:43:02","date_gmt":"2021-05-14T03:43:02","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1560"},"modified":"2021-10-14T03:43:52","modified_gmt":"2021-10-14T03:43:52","slug":"pandas-sort-custom","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/05\/pandas-sort-custom\/","title":{"rendered":"Pandas\u6392\u5e8f\uff0c\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u81ea\u5b9a\u4e49\u6392\u5e8f"},"content":{"rendered":"

Pandas\u6392\u5e8f\uff0c\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u81ea\u5b9a\u4e49\u6392\u5e8f.md
\n<\/p>\n

Pandas\u7684sort_index\u548csort_values<\/h2>\n

\u6211\u4eec\u77e5\u9053pandas\u6709sort_index\u548csort_values\u4e24\u4e2a\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u901a\u8fc7\u6848\u4f8b\u6765\u56de\u987e\u4e0b<\/p>\n

\n
>>> datalist = [{'name': 'PC', 'value': 0.38}, {'name': 'PAD', 'value': 0.13}, {'name': 'PHONE', 'value': 0.49 }]\n>>> df = pd.DataFrame(datalist)\n>>> print(df)\n0     PC   0.38\n1    PAD   0.13\n2  PHONE   0.49\n\n>>> print(df.sort_values(by=['value', 'name'], ascending=False))\n    name  value\n2  PHONE   0.49\n0     PC   0.38\n1    PAD   0.13\n\n>>> newdf= df.set_index('name')\n>>> print(newdf)\n       value\nname        \nPC      0.38\nPAD     0.13\nPHONE   0.49\n\n>>> print(newdf.sort_index(ascending=False))\n       value\nname        \nPHONE   0.49\nPC      0.38\nPAD     0.13<\/code><\/pre>\n<\/div>\n

\u4f46\u662f\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u60f3\u8981\u6309\u7167\u4e00\u4e9b\u7279\u5b9a\u7684\u987a\u5e8f\u6765\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5982\u4e0b\u7684\u65b9\u6cd5<\/p>\n

\n
>>> sort_list = ['PC', 'PHONE', 'PAD']\n>>> print(newdf.loc[sort_list])\n       value\nname        \nPC      0.38\nPHONE   0.49\nPAD     0.13<\/code><\/pre>\n<\/div>\n

\u7b49\u7b49\uff0c\u8fd9\u4e2aloc\u662f\u6392\u5e8f\u4e48\uff1f\u4e0d\u662f\u54c8\uff0c\u8fd9\u4e2a\u662f\u53d6\u51fa\u7279\u5b9a\u884c\u3002\u4f46\u786e\u5b9e\u662f\u5b9e\u73b0\u4e86\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002<\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Pandas\u6392\u5e8f\uff0c\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u81ea\u5b9a\u4e49\u6392\u5e8f<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Pandas\u6392\u5e8f\uff0c\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u81ea\u5b9a\u4e49\u6392\u5e8f.md Pandas\u7684sort_index\u548csort_values \u6211\u4eec\u77e5\u9053pandas\u6709sort_index\u548csort_values\u4e24\u4e2a\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u901a\u8fc7\u6848\u4f8b\u6765\u56de\u987e\u4e0b >>> datalist = [{‘name’: ‘PC’, ‘value’: 0.38}, {‘name’: ‘PAD’, ‘value’: 0.13}, {‘name’: ‘PHONE’, ‘value’: 0.49 }] >>> df = pd.DataFrame(datalist) >>> print(df) 0 PC 0.38 1 PAD 0.13 2 PHONE 0.49 >>> print(df.sort_values(by=[‘value’, ‘name’], ascending=False)) name value 2 PHONE 0.49 0 PC 0.38 1 PAD 0.13 >>> newdf= df.set_index(‘name’) >>> print(newdf) value […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[220,59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1560"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1560"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1560\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1560\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}