{"id":1154046,"date":"2025-01-13T17:46:23","date_gmt":"2025-01-13T09:46:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154046.html"},"modified":"2025-01-13T17:46:25","modified_gmt":"2025-01-13T09:46:25","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%88%97%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154046.html","title":{"rendered":"python\u5982\u4f55\u53bb\u9664\u5217\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183724\/996f52b8-451f-437f-bc0b-14c28bead496.webp\" alt=\"python\u5982\u4f55\u53bb\u9664\u5217\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53bb\u9664\u5217\u7d22\u5f15\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684\u5404\u79cd\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\u901a\u8fc7<code>reset_index()<\/code>\u3001<code>drop()<\/code>\u3001<code>set_index()<\/code>\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528reset_index()\u91cd\u7f6e\u7d22\u5f15\u3001drop()\u5220\u9664\u4e0d\u9700\u8981\u7684\u7d22\u5f15\u3001set_index()\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7reset_index()\u91cd\u7f6e\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>reset_index()<\/code>\u65b9\u6cd5\u7528\u4e8e\u91cd\u7f6e\u7d22\u5f15\uff0c\u5c06\u7d22\u5f15\u5217\u8f6c\u6362\u4e3a\u666e\u901a\u5217\uff0c\u5e76\u751f\u6210\u9ed8\u8ba4\u7684\u6574\u6570\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index(&#39;A&#39;, inplace=True)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u91cd\u7f6e\u7d22\u5f15<\/strong><\/h2>\n<p>df_reset = df.reset_index()<\/p>\n<p>print(&quot;\\n\u91cd\u7f6e\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reset)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217<code>&#39;A&#39;<\/code>\u8bbe\u7f6e\u4e3a\u7d22\u5f15\uff0c\u7136\u540e\u901a\u8fc7<code>reset_index()<\/code>\u65b9\u6cd5\u5c06\u5176\u91cd\u7f6e\u4e3a\u9ed8\u8ba4\u7684\u6574\u6570\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7drop()\u5220\u9664\u4e0d\u9700\u8981\u7684\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>drop()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6307\u5b9a\u7684\u5217\u3002\u82e5\u67d0\u5217\u5df2\u7ecf\u4f5c\u4e3a\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7<code>reset_index()<\/code>\u65b9\u6cd5\u5148\u91cd\u7f6e\u7d22\u5f15\uff0c\u518d\u4f7f\u7528<code>drop()<\/code>\u65b9\u6cd5\u5220\u9664\u4e0d\u9700\u8981\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index(&#39;A&#39;, inplace=True)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u91cd\u7f6e\u7d22\u5f15\u5e76\u5220\u9664\u4e0d\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>df_reset = df.reset_index().drop(columns=[&#39;A&#39;])<\/p>\n<p>print(&quot;\\n\u5220\u9664\u7d22\u5f15\u5217\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reset)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217<code>&#39;A&#39;<\/code>\u8bbe\u7f6e\u4e3a\u7d22\u5f15\uff0c\u7136\u540e\u901a\u8fc7<code>reset_index()<\/code>\u65b9\u6cd5\u91cd\u7f6e\u7d22\u5f15\uff0c\u5e76\u4f7f\u7528<code>drop()<\/code>\u65b9\u6cd5\u5220\u9664\u5217<code>&#39;A&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7set_index()\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>set_index()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u6307\u5b9a\u7684\u5217\u8bbe\u7f6e\u4e3a\u7d22\u5f15\uff0c\u82e5\u4e0d\u9700\u8981\u7d22\u5f15\u5217\uff0c\u53ef\u4ee5\u5c06\u7d22\u5f15\u8bbe\u7f6e\u4e3a\u7a7a\u6216\u5176\u4ed6\u5217\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index(&#39;A&#39;, inplace=True)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15<\/strong><\/h2>\n<p>df_new_index = df.set_index(pd.Index([0, 1, 2]))<\/p>\n<p>print(&quot;\\n\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_new_index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>set_index()<\/code>\u65b9\u6cd5\u5c06\u7d22\u5f15\u8bbe\u7f6e\u4e3a\u7a7a\u7684\u7d22\u5f15\u6216\u5176\u4ed6\u65b0\u751f\u6210\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5220\u9664\u591a\u7ea7\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ea7\u7d22\u5f15\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>reset_index()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u7279\u5b9a\u7ea7\u522b\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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 = {<\/p>\n<p>    &#39;A&#39;: [&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;, &#39;qux&#39;],<\/p>\n<p>    &#39;B&#39;: [&#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;four&#39;],<\/p>\n<p>    &#39;C&#39;: [1, 2, 3, 4],<\/p>\n<p>    &#39;D&#39;: [5, 6, 7, 8]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index([&#39;A&#39;, &#39;B&#39;], inplace=True)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u91cd\u7f6e\u7279\u5b9a\u7ea7\u522b\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>df_reset_level = df.reset_index(level=&#39;A&#39;)<\/p>\n<p>print(&quot;\\n\u91cd\u7f6e\u7279\u5b9a\u7ea7\u522b\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reset_level)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06<code>&#39;A&#39;<\/code>\u548c<code>&#39;B&#39;<\/code>\u5217\u8bbe\u7f6e\u4e3a\u591a\u7ea7\u7d22\u5f15\uff0c\u7136\u540e\u901a\u8fc7<code>reset_index(level=&#39;A&#39;)<\/code>\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u7ea7\u522b\u7684\u7d22\u5f15<code>&#39;A&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5c42\u7ea7\u7d22\u5f15\u7684DataFrame<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5c42\u7ea7\u7d22\u5f15\uff08MultiIndex\uff09\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5220\u9664\u67d0\u4e9b\u5c42\u7ea7\u7684\u7d22\u5f15\u6216\u8005\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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 = {<\/p>\n<p>    &#39;A&#39;: [&#39;foo&#39;, &#39;foo&#39;, &#39;bar&#39;, &#39;bar&#39;],<\/p>\n<p>    &#39;B&#39;: [&#39;one&#39;, &#39;two&#39;, &#39;one&#39;, &#39;two&#39;],<\/p>\n<p>    &#39;C&#39;: [1, 2, 3, 4],<\/p>\n<p>    &#39;D&#39;: [5, 6, 7, 8]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index([&#39;A&#39;, &#39;B&#39;], inplace=True)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u91cd\u7f6e\u591a\u7ea7\u7d22\u5f15<\/strong><\/h2>\n<p>df_reset = df.reset_index()<\/p>\n<p>print(&quot;\\n\u91cd\u7f6e\u591a\u7ea7\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reset)<\/p>\n<h2><strong>\u5220\u9664\u7279\u5b9a\u7ea7\u522b\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>df_reset_level = df.reset_index(level=&#39;A&#39;)<\/p>\n<p>print(&quot;\\n\u5220\u9664\u7279\u5b9a\u7ea7\u522b\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reset_level)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06<code>&#39;A&#39;<\/code>\u548c<code>&#39;B&#39;<\/code>\u5217\u8bbe\u7f6e\u4e3a\u591a\u7ea7\u7d22\u5f15\uff0c\u7136\u540e\u901a\u8fc7<code>reset_index()<\/code>\u65b9\u6cd5\u91cd\u7f6e\u591a\u7ea7\u7d22\u5f15\uff0c\u6216\u8005\u901a\u8fc7<code>reset_index(level=&#39;A&#39;)<\/code>\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u7ea7\u522b\u7684\u7d22\u5f15<code>&#39;A&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>iloc<\/code>\u548c<code>loc<\/code>\u65b9\u6cd5\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>iloc<\/code>\u548c<code>loc<\/code>\u65b9\u6cd5\u5bf9DataFrame\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\uff0c\u5220\u9664\u7279\u5b9a\u7684\u884c\u6216\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u65b9\u6cd5\u5220\u9664\u7b2c\u4e00\u5217<\/strong><\/h2>\n<p>df_drop_iloc = df.iloc[:, 1:]<\/p>\n<p>print(&quot;\\n\u4f7f\u7528iloc\u65b9\u6cd5\u5220\u9664\u7b2c\u4e00\u5217\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_drop_iloc)<\/p>\n<h2><strong>\u4f7f\u7528loc\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5217<\/strong><\/h2>\n<p>df_drop_loc = df.loc[:, df.columns != &#39;A&#39;]<\/p>\n<p>print(&quot;\\n\u4f7f\u7528loc\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5217\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_drop_loc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>iloc<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u7b2c\u4e00\u5217\uff0c\u4f7f\u7528<code>loc<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u7279\u5b9a\u5217<code>&#39;A&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528<code>pop<\/code>\u65b9\u6cd5\u5220\u9664\u5217<\/h3>\n<\/p>\n<p><p><code>pop<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664DataFrame\u4e2d\u7684\u7279\u5b9a\u5217\uff0c\u5e76\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528pop\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5217<\/strong><\/h2>\n<p>df_pop = df.copy()<\/p>\n<p>df_pop.pop(&#39;A&#39;)<\/p>\n<p>print(&quot;\\n\u4f7f\u7528pop\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5217\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_pop)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pop<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u5217<code>&#39;A&#39;<\/code>\uff0c\u5e76\u8fd4\u56de\u5220\u9664\u7684\u5217\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<code>drop<\/code>\u65b9\u6cd5\u5220\u9664\u884c\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5220\u9664\u5217\u7d22\u5f15\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>drop<\/code>\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u7684\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528drop\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>df_drop_row = df.drop(index=1)<\/p>\n<p>print(&quot;\\n\u4f7f\u7528drop\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u884c\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_drop_row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>drop<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u884c\u7d22\u5f15\u4e3a1\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5220\u9664\u91cd\u590d\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5220\u9664\u91cd\u590d\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 2, 3], &#39;B&#39;: [4, 5, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>df_unique = df.drop_duplicates(subset=[&#39;A&#39;])<\/p>\n<p>print(&quot;\\n\u5220\u9664\u91cd\u590d\u884c\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_unique)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>drop_duplicates<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u91cd\u590d\u7684\u884c\u7d22\u5f15<code>&#39;A&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u901a\u8fc7<code>assign<\/code>\u65b9\u6cd5\u91cd\u65b0\u5206\u914d\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>assign<\/code>\u65b9\u6cd5\u91cd\u65b0\u5206\u914d\u65b0\u7684\u7d22\u5f15\u5217\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528assign\u65b9\u6cd5\u91cd\u65b0\u5206\u914d\u7d22\u5f15\u5217<\/strong><\/h2>\n<p>df_assign = df.assign(New_Index=[0, 1, 2])<\/p>\n<p>print(&quot;\\n\u4f7f\u7528assign\u65b9\u6cd5\u91cd\u65b0\u5206\u914d\u7d22\u5f15\u5217\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_assign)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>assign<\/code>\u65b9\u6cd5\u5206\u914d\u4e86\u65b0\u7684\u7d22\u5f15\u5217<code>&#39;New_Index&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528<code>reindex<\/code>\u65b9\u6cd5\u91cd\u65b0\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>reindex<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u91cd\u65b0\u7d22\u5f15DataFrame\uff0c\u5e76\u53ef\u4ee5\u586b\u5145\u7f3a\u5931\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528reindex\u65b9\u6cd5\u91cd\u65b0\u7d22\u5f15<\/strong><\/h2>\n<p>df_reindex = df.reindex([0, 2])<\/p>\n<p>print(&quot;\\n\u4f7f\u7528reindex\u65b9\u6cd5\u91cd\u65b0\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_reindex)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>reindex<\/code>\u65b9\u6cd5\u91cd\u65b0\u7d22\u5f15\uff0c\u9009\u62e9\u4e86\u7279\u5b9a\u7684\u884c\u7d22\u5f15<code>[0, 2]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528<code>filter<\/code>\u65b9\u6cd5\u7b5b\u9009\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>filter<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u7279\u5b9a\u7684\u884c\u6216\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528filter\u65b9\u6cd5\u7b5b\u9009\u5217\u7d22\u5f15<\/strong><\/h2>\n<p>df_filter = df.filter(items=[&#39;B&#39;])<\/p>\n<p>print(&quot;\\n\u4f7f\u7528filter\u65b9\u6cd5\u7b5b\u9009\u5217\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_filter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>filter<\/code>\u65b9\u6cd5\u7b5b\u9009\u4e86\u7279\u5b9a\u7684\u5217\u7d22\u5f15<code>&#39;B&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528<code>rename<\/code>\u65b9\u6cd5\u91cd\u547d\u540d\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>rename<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u91cd\u547d\u540dDataFrame\u7684\u884c\u6216\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528rename\u65b9\u6cd5\u91cd\u547d\u540d\u5217\u7d22\u5f15<\/strong><\/h2>\n<p>df_rename = df.rename(columns={&#39;A&#39;: &#39;Alpha&#39;, &#39;B&#39;: &#39;Beta&#39;})<\/p>\n<p>print(&quot;\\n\u4f7f\u7528rename\u65b9\u6cd5\u91cd\u547d\u540d\u5217\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_rename)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>rename<\/code>\u65b9\u6cd5\u91cd\u547d\u540d\u4e86\u5217\u7d22\u5f15\uff0c\u5c06<code>&#39;A&#39;<\/code>\u6539\u4e3a<code>&#39;Alpha&#39;<\/code>\uff0c<code>&#39;B&#39;<\/code>\u6539\u4e3a<code>&#39;Beta&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528<code>set_axis<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>set_axis<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6eDataFrame\u7684\u884c\u6216\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528set_axis\u65b9\u6cd5\u8bbe\u7f6e\u5217\u7d22\u5f15<\/strong><\/h2>\n<p>df_set_axis = df.set_axis([&#39;Alpha&#39;, &#39;Beta&#39;], axis=&#39;columns&#39;)<\/p>\n<p>print(&quot;\\n\u4f7f\u7528set_axis\u65b9\u6cd5\u8bbe\u7f6e\u5217\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_set_axis)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>set_axis<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u65b0\u7684\u5217\u7d22\u5f15<code>[&#39;Alpha&#39;, &#39;Beta&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528<code>melt<\/code>\u65b9\u6cd5\u8f6c\u6362\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>melt<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06DataFrame\u4ece\u5bbd\u683c\u5f0f\u8f6c\u6362\u4e3a\u957f\u683c\u5f0f\uff0c\u4ece\u800c\u6539\u53d8\u7d22\u5f15\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528melt\u65b9\u6cd5\u8f6c\u6362\u7d22\u5f15<\/strong><\/h2>\n<p>df_melt = df.melt(id_vars=[&#39;A&#39;], value_vars=[&#39;B&#39;], var_name=&#39;Variable&#39;, value_name=&#39;Value&#39;)<\/p>\n<p>print(&quot;\\n\u4f7f\u7528melt\u65b9\u6cd5\u8f6c\u6362\u7d22\u5f15\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df_melt)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>melt<\/code>\u65b9\u6cd5\u5c06DataFrame\u4ece\u5bbd\u683c\u5f0f\u8f6c\u6362\u4e3a\u957f\u683c\u5f0f\uff0c\u4ece\u800c\u6539\u53d8\u4e86\u7d22\u5f15\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u548c\u7ba1\u7406Python DataFrame\u4e2d\u7684\u5217\u7d22\u5f15\u548c\u884c\u7d22\u5f15\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u53bb\u9664DataFrame\u7684\u5217\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664DataFrame\u7684\u5217\u7d22\u5f15\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>DataFrame.columns<\/code>\u4e3a<code>None<\/code>\u6216\u8005\u4f7f\u7528<code>DataFrame.reset_index(drop=True, axis=1)<\/code>\u6765\u5b9e\u73b0\u3002\u8fd9\u6837\u505a\u4f1a\u5c06\u5217\u7d22\u5f15\u66ff\u6362\u4e3a\u9ed8\u8ba4\u7684\u6574\u6570\u7d22\u5f15\uff0c\u4ece\u800c\u8fbe\u5230\u53bb\u9664\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u53bb\u9664\u5217\u7d22\u5f15\u540e\uff0c\u5982\u4f55\u4fdd\u5b58DataFrame\uff1f<\/strong><br \/>\u53bb\u9664\u5217\u7d22\u5f15\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.to_csv()<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u5728\u4fdd\u5b58\u65f6\uff0c\u786e\u4fdd\u8bbe\u7f6e<code>index=False<\/code>\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5c06\u7d22\u5f15\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a<code>df.to_csv(&#39;filename.csv&#39;, index=False)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4e2d\u53ea\u6709\u6570\u636e\uff0c\u6ca1\u6709\u7d22\u5f15\u5217\u3002<\/p>\n<p><strong>\u53bb\u9664\u5217\u7d22\u5f15\u5bf9\u6570\u636e\u5206\u6790\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\u53bb\u9664\u5217\u7d22\u5f15\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u5206\u6790\u7684\u4fbf\u5229\u6027\u3002\u6ca1\u6709\u5217\u7d22\u5f15\u65f6\uff0c\u53ef\u80fd\u96be\u4ee5\u8bc6\u522b\u6570\u636e\u7684\u542b\u4e49\uff0c\u56e0\u6b64\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u51b3\u5b9a\u662f\u5426\u9700\u8981\u53bb\u9664\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u6570\u636e\u53ef\u89c6\u5316\u65f6\u6dfb\u52a0\u9002\u5f53\u7684\u6807\u7b7e\uff0c\u4ee5\u4fbf\u4e8e\u7406\u89e3\u548c\u89e3\u91ca\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53bb\u9664\u5217\u7d22\u5f15\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684\u5404\u79cd\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\u901a\u8fc7reset_i [&hellip;]","protected":false},"author":3,"featured_media":1154052,"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\/1154046"}],"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=1154046"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154046\/revisions"}],"predecessor-version":[{"id":1154055,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154046\/revisions\/1154055"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154052"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}