{"id":1107321,"date":"2025-01-08T16:48:32","date_gmt":"2025-01-08T08:48:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107321.html"},"modified":"2025-01-08T16:48:35","modified_gmt":"2025-01-08T08:48:35","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9pd%e9%a1%ba%e5%ba%8f%e5%8f%8d%e4%b8%80%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1107321.html","title":{"rendered":"Python\u5982\u4f55\u8ba9pd\u987a\u5e8f\u53cd\u4e00\u4e0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071352\/9fe8a735-a63b-4ebf-90b2-25da43481b4a.webp\" alt=\"Python\u5982\u4f55\u8ba9pd\u987a\u5e8f\u53cd\u4e00\u4e0b\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4e2d\u7684Pandas\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528<code>iloc<\/code>\u7d22\u5f15\u3001<code>reindex<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca<code>sort_index<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>iloc<\/code>\u7d22\u5f15\uff0c\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528iloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><code>iloc<\/code>\u662fPandas\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u7d22\u5f15\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u4fee\u6539DataFrame\u3002\u8981\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>iloc[::-1]<\/code>\uff0c\u8fd9\u4f1a\u5c06DataFrame\u7684\u884c\u987a\u5e8f\u53cd\u8f6c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u64cd\u4f5c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.iloc[::-1].reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8bDataFrame\uff0c\u7136\u540e\u901a\u8fc7<code>iloc[::-1]<\/code>\u5c06\u5176\u884c\u987a\u5e8f\u53cd\u8f6c\uff0c\u5e76\u4f7f\u7528<code>reset_index(drop=True)<\/code>\u65b9\u6cd5\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u5220\u9664\u65e7\u7d22\u5f15\u3002\u6700\u7ec8\u8f93\u51fa\u7684DataFrame\u987a\u5e8f\u5df2\u7ecf\u88ab\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528reindex\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><code>reindex<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u6839\u636e\u65b0\u7684\u7d22\u5f15\u91cd\u65b0\u6392\u5217DataFrame\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7d22\u5f15\uff0c\u8be5\u7d22\u5f15\u662f\u539f\u59cb\u7d22\u5f15\u7684\u53cd\u5e8f\uff0c\u7136\u540e\u4f7f\u7528<code>reindex<\/code>\u65b9\u6cd5\u91cd\u65b0\u6392\u5217DataFrame\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528reindex\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/p>\n<p>df_reversed = df.reindex(index=df.index[::-1]).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>iloc<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c<code>reindex<\/code>\u65b9\u6cd5\u901a\u8fc7\u4f20\u5165\u53cd\u5e8f\u7684\u7d22\u5f15\u6765\u91cd\u65b0\u6392\u5217DataFrame\uff0c\u5e76\u4f7f\u7528<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528sort_index\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><code>sort_index<\/code>\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u6839\u636e\u7d22\u5f15\u6392\u5e8fDataFrame\u3002\u901a\u8fc7\u5c06<code>ascending<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7d22\u5f15\u7684\u53cd\u8f6c\uff0c\u4ece\u800c\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sort_index\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/p>\n<p>df_reversed = df.sort_index(ascending=False).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5bf9\u7d22\u5f15\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u6765\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\uff0c\u6700\u7ec8\u6548\u679c\u4e0e\u524d\u4e24\u79cd\u65b9\u6cd5\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528iloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>iloc<\/code>\u662fPandas\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u7d22\u5f15\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u4fee\u6539DataFrame\u3002\u8981\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>iloc[::-1]<\/code>\uff0c\u8fd9\u4f1a\u5c06DataFrame\u7684\u884c\u987a\u5e8f\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.iloc[::-1].reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>iloc[::-1]<\/code>\u5c06DataFrame\u7684\u884c\u987a\u5e8f\u53cd\u8f6c\u3002<code>iloc<\/code>\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6574\u6570\u7d22\u5f15\u6765\u8bbf\u95eeDataFrame\u7684\u884c\u548c\u5217\uff0c<code>[::-1]<\/code>\u8868\u793a\u53cd\u5411\u5207\u7247\u3002\u901a\u8fc7<code>reset_index(drop=True)<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u5e76\u5220\u9664\u65e7\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528reindex\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>reindex<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u6839\u636e\u65b0\u7684\u7d22\u5f15\u91cd\u65b0\u6392\u5217DataFrame\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7d22\u5f15\uff0c\u8be5\u7d22\u5f15\u662f\u539f\u59cb\u7d22\u5f15\u7684\u53cd\u5e8f\uff0c\u7136\u540e\u4f7f\u7528<code>reindex<\/code>\u65b9\u6cd5\u91cd\u65b0\u6392\u5217DataFrame\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528reindex\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.reindex(index=df.index[::-1]).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7<code>df.index[::-1]<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u53cd\u5e8f\u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<code>reindex<\/code>\u65b9\u6cd5\uff0c\u4ece\u800c\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u5e76\u5220\u9664\u65e7\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528sort_index\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>sort_index<\/code>\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u6839\u636e\u7d22\u5f15\u6392\u5e8fDataFrame\u3002\u901a\u8fc7\u5c06<code>ascending<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7d22\u5f15\u7684\u53cd\u8f6c\uff0c\u4ece\u800c\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528sort_index\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.sort_index(ascending=False).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7\u5c06<code>ascending<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>False<\/code>\u6765\u8c03\u7528<code>sort_index<\/code>\u65b9\u6cd5\uff0c\u4ece\u800c\u5bf9\u7d22\u5f15\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff0c\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u5e76\u5220\u9664\u65e7\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528iloc\u548cloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>iloc<\/code>\u548c<code>loc<\/code>\u7d22\u5f15\u7ed3\u5408\u6765\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002<code>iloc<\/code>\u548c<code>loc<\/code>\u7d22\u5f15\u90fd\u53ef\u4ee5\u901a\u8fc7\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u4fee\u6539DataFrame\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u548cloc\u7d22\u5f15\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.iloc[::-1].loc[:, ::-1].reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7<code>iloc[::-1]<\/code>\u5c06DataFrame\u7684\u884c\u987a\u5e8f\u53cd\u8f6c\uff0c\u5e76\u901a\u8fc7<code>loc[:, ::-1]<\/code>\u5c06DataFrame\u7684\u5217\u987a\u5e8f\u53cd\u8f6c\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u5e76\u5220\u9664\u65e7\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528flipud\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>flipud<\/code>\u65b9\u6cd5\u662fNumPy\u5e93\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u53cd\u8f6c\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u901a\u8fc7\u5c06DataFrame\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>flipud<\/code>\u65b9\u6cd5\u6765\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528flipud\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = pd.DataFrame(np.flipud(df.values), columns=df.columns).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7\u5c06DataFrame\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>flipud<\/code>\u65b9\u6cd5\u53cd\u8f6c\u5176\u987a\u5e8f\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>pd.DataFrame<\/code>\u5c06\u53cd\u8f6c\u540e\u7684NumPy\u6570\u7ec4\u8f6c\u6362\u56deDataFrame\uff0c\u5e76\u4f7f\u7528<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528iterrows\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>iterrows<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u9010\u884c\u904d\u5386DataFrame\u3002\u901a\u8fc7\u5c06\u904d\u5386\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u53cd\u8f6c\u8be5\u5217\u8868\uff0c\u53ef\u4ee5\u5b9e\u73b0DataFrame\u7684\u987a\u5e8f\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iterrows\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>rows = [row for row in df.iterrows()]<\/p>\n<p>rows_reversed = rows[::-1]<\/p>\n<p>df_reversed = pd.DataFrame([row[1] for row in rows_reversed], columns=df.columns).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7<code>iterrows<\/code>\u65b9\u6cd5\u9010\u884c\u904d\u5386DataFrame\uff0c\u5e76\u5c06\u904d\u5386\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u53cd\u8f6c\u8be5\u5217\u8868\uff0c\u5e76\u901a\u8fc7<code>pd.DataFrame<\/code>\u5c06\u53cd\u8f6c\u540e\u7684\u5217\u8868\u8f6c\u6362\u56deDataFrame\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528apply\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p><code>apply<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u5bf9DataFrame\u7684\u6bcf\u4e00\u884c\u6216\u6bcf\u4e00\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u53cd\u8f6c\u51fd\u6570\uff0c\u5e76\u4f7f\u7528<code>apply<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0DataFrame\u7684\u987a\u5e8f\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;Age&#39;: [24, 30, 22, 29]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u53cd\u8f6c\u51fd\u6570<\/strong><\/h2>\n<p>def reverse_df(df):<\/p>\n<p>    return df.iloc[::-1]<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f<\/strong><\/h2>\n<p>df_reversed = df.apply(reverse_df, axis=0).reset_index(drop=True)<\/p>\n<p>print(df_reversed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53cd\u8f6c\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528<code>iloc[::-1]<\/code>\u5c06DataFrame\u7684\u884c\u987a\u5e8f\u53cd\u8f6c\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7<code>apply<\/code>\u65b9\u6cd5\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8eDataFrame\u7684\u6bcf\u4e00\u5217\uff08<code>axis=0<\/code>\u8868\u793a\u6309\u5217\u5e94\u7528\uff09\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>reset_index(drop=True)<\/code>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u53cd\u8f6cPandas DataFrame\u7684\u987a\u5e8f\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>iloc<\/code>\u7d22\u5f15\u3001<code>reindex<\/code>\u65b9\u6cd5\u3001<code>sort_index<\/code>\u65b9\u6cd5\u3001<code>flipud<\/code>\u65b9\u6cd5\u3001<code>iterrows<\/code>\u65b9\u6cd5\u8fd8\u662f<code>apply<\/code>\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\u66f4\u52a0\u7075\u6d3b\u5730\u64cd\u4f5cDataFrame\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684pandas\u5e93\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528pandas\u5e93\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>iloc<\/code>\u6216<code>loc<\/code>\u5c5e\u6027\uff0c\u7ed3\u5408\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>df.iloc[::-1]<\/code>\u53ef\u4ee5\u76f4\u63a5\u53cd\u8f6cDataFrame\u7684\u884c\u987a\u5e8f\u3002\u6b64\u5916\uff0c<code>df[::-1]<\/code>\u4e5f\u53ef\u4ee5\u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\u3002\u53cd\u8f6c\u540e\u7684DataFrame\u4f1a\u4fdd\u7559\u539f\u6709\u7684\u7d22\u5f15\uff0c\u4f46\u884c\u7684\u987a\u5e8f\u5c06\u88ab\u98a0\u5012\u3002<\/p>\n<p><strong>\u5728\u53cd\u8f6cDataFrame\u65f6\uff0c\u5982\u4f55\u4fdd\u7559\u539f\u6765\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5f53\u4f60\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cpandas\u4f1a\u4fdd\u7559\u539f\u6709\u7684\u7d22\u5f15\u3002\u5982\u679c\u5e0c\u671b\u7d22\u5f15\u5728\u53cd\u8f6c\u540e\u91cd\u65b0\u6392\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>reset_index()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7<code>df.iloc[::-1].reset_index(drop=True)<\/code>\uff0c\u53ef\u4ee5\u5728\u53cd\u8f6c\u540e\u751f\u6210\u4e00\u4e2a\u65b0\u7684DataFrame\uff0c\u5176\u7d22\u5f15\u5c06\u4ece0\u5f00\u59cb\u91cd\u65b0\u7f16\u53f7\u3002<\/p>\n<p><strong>\u5982\u4f55\u53cd\u8f6cSeries\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u53cd\u8f6cpandas\u4e2d\u7684Series\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u3002\u901a\u8fc7<code>series[::-1]<\/code>\u53ef\u4ee5\u8f7b\u677e\u53cd\u8f6cSeries\u7684\u987a\u5e8f\u3002\u6b64\u64cd\u4f5c\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684Series\uff0c\u5305\u542b\u539fSeries\u7684\u5143\u7d20\uff0c\u4f46\u987a\u5e8f\u4e3a\u76f8\u53cd\u3002\u5982\u679c\u5e0c\u671b\u5728\u53cd\u8f6c\u540e\u91cd\u7f6e\u7d22\u5f15\uff0c\u53ef\u4ee5\u540c\u6837\u4f7f\u7528<code>reset_index(drop=True)<\/code>\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4e2d\u7684Pandas\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u53cd\u8f6cDataFrame\u7684\u987a\u5e8f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1107329,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107321"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1107321"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107321\/revisions"}],"predecessor-version":[{"id":1107333,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107321\/revisions\/1107333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107329"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}