{"id":195067,"date":"2020-07-08T10:00:37","date_gmt":"2020-07-08T02:00:37","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=195067"},"modified":"2020-06-28T08:57:22","modified_gmt":"2020-06-28T00:57:22","slug":"python-linux-inplace","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-linux-inplace.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdPython drop\u65b9\u6cd5\u5220\u9664\u5217\u4e4binplace\u53c2\u6570\u5b9e\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python drop\u65b9\u6cd5\u5220\u9664\u5217\u4e4binplace\u53c2\u6570\u5b9e\u4f8b\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u4e00\u8d77\u8ddf\u968f\u5c0f\u7f16\u8fc7\u6765\u770b\u770b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

drop\u65b9\u6cd5\u6709\u4e00\u4e2a\u53ef\u9009\u53c2\u6570inplace\uff0c\u8868\u660e\u53ef\u5bf9\u539f\u6570\u7ec4\u4f5c\u51fa\u4fee\u6539\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u4e0d\u7ba1\u53c2\u6570\u9ed8\u8ba4\u4e3aFalse\u8fd8\u662f\u8bbe\u7f6e\u4e3aTrue\uff0c\u539f\u6570\u7ec4\u7684\u5185\u5b58\u503c\u662f\u4e0d\u4f1a\u6539\u53d8\u7684\uff0c\u533a\u522b\u5728\u4e8e\u539f\u6570\u7ec4\u7684\u5185\u5bb9\u662f\u5426\u76f4\u63a5\u88ab\u4fee\u6539\u3002\u9ed8\u8ba4\u4e3aFalse,\u8868\u660e\u539f\u6570\u7ec4\u5185\u5bb9\u5e76\u4e0d\u6539\u53d8\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5f97\u5230\u6539\u53d8\u540e\u7684\u5185\u5bb9\uff0c\u9700\u8981\u5c06\u65b0\u7ed3\u679c\u8d4b\u7ed9\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5373data = data.drop(['test','test2'],1)\u3002<\/p>\n

\u5982\u679c\u5c06inplace\u503c\u8bbe\u5b9a\u4e3aTrue,\u5219\u539f\u6570\u7ec4\u5185\u5bb9\u76f4\u63a5\u88ab\u6539\u53d8\u3002<\/p>\n

\u6d4b\u8bd5\u7a0b\u5e8f\u5982\u4e0b<\/p>\n

\r\n#\u589e\u52a0\u4e24\u5217\u7a7a\u503c\r\nimport numpy as np\r\ndata[\"test\"] = np.nan\r\ndata[\"test2\"] = np.nan\r\n  \r\nname    gender  age test2   test\r\n0   jerry   M   36  NaN NaN\r\n1   emma    F   23  NaN NaN\r\n2   tony    M   34  NaN NaN\r\n4   bob M   20  NaN NaN\r\n  \r\n#\u67e5\u770b\u6b64\u65f6data\u7684\u5185\u5b58\u5730\u5740\r\nid(data)\r\n128971088\r\n  \r\n#\u5220\u9664\u8fd9\u4e24\u5217\uff0cinplace\u9ed8\u8ba4\u4e3aFalse\r\nid(data.drop(['test','test2'],1))\r\n128971888\r\n  \r\n#\u67e5\u770bdata,\u53d1\u73b0\u6570\u636e\u5e76\u672a\u6539\u53d8\r\ndata\r\nname    gender  age test    test2\r\n0   jerry   M   36  NaN NaN\r\n1   emma    F   23  NaN NaN\r\n2   tony    M   34  NaN NaN\r\n4   bob M   20  NaN NaN\r\n  \r\n#\u67e5\u770bdata\u7684ID\r\nid(data)\r\n128971088\r\n  \r\n#\u5220\u9664\u8fd9\u4e24\u5217\uff0cinplace\u8bbe\u7f6e\u4e3aFalse\r\nid(data.drop(['test','test2'],1,inplace = True))\r\n1545984728\r\n  \r\n#\u67e5\u770bdata,\u6570\u636e\u5df2\u7ecf\u6539\u53d8\r\ndata\r\nname    gender  age test    test2\r\n0   jerry   M   36  NaN NaN\r\n1   emma    F   23  NaN NaN\r\n2   tony    M   34  NaN NaN\r\n4   bob M   20  NaN NaN\r\n  \r\n#\u67e5\u770bdata\u7684ID\r\nid(data)\r\n128971088<\/pre>\n
\u8865\u5145\u77e5\u8bc6\uff1apython \u4f7f\u7528del\u548cdrop\u65b9\u6cd5\u5220\u9664DataFrame\u7684\u5217\uff0c\u4f7f\u7528drop\u65b9\u6cd5\u4e00\u6b21\u5220\u9664\u591a\u5217<\/strong><\/span><\/div>\n

\u4f7f\u7528del\u548cdrop\u65b9\u6cd5\u5220\u9664DataFrame\u4e2d\u7684\u5217\uff0c\u4f7f\u7528drop\u65b9\u6cd5\u4e00\u6b21\u5220\u9664\u591a\u5217<\/p>\n

\r\n# \u4f7f\u7528del, \u4e00\u6b21\u53ea\u80fd\u5220\u9664\u4e00\u5217\uff0c\u4e0d\u80fd\u4e00\u6b21\u5220\u9664\u591a\u5217\r\n# \u53ea\u80fd\u4f7f\u7528 del df['\u5bc6\u5ea6'], \u4e0d\u80fd\u4f7f\u7528 del df[['\u5bc6\u5ea6', '\u542b\u7cd6\u7387']]\r\n  \r\ndel df['\u5bc6\u5ea6']\r\n  \r\n# del df[['\u5bc6\u5ea6', '\u542b\u7cd6\u7387']] \u62a5\u9519\r\n# \u4f7f\u7528drop\uff0c\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\r\n  \r\ndt = dt.drop(['\u5bc6\u5ea6', '\u542b\u7cd6\u7387'], axis=1) # axis=1 \u8868\u793a\u5220\u9664\u5217\uff0c['\u5bc6\u5ea6', '\u542b\u7cd6\u7387'] \u8981\u5220\u9664\u7684col\u7684\u5217\u8868\uff0c\u53ef\u4e00\u6b21\u5220\u9664\u591a\u5217\r\n  \r\ndt.drop(['\u5bc6\u5ea6', '\u542b\u7cd6\u7387'], axis=1, inplace=True) # inplace=True, \u76f4\u63a5\u4ece\u5185\u90e8\u5220\u9664\r\n  \r\ndt.drop(dt.columns[[0, 4, 8]], axis=1, inplace=True) # dt.columns[[0, 4, 8]] \u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u67e5\u627e\u5217<\/pre>\n

\u4ee5\u4e0a\u8fd9\u7bc7Python drop\u65b9\u6cd5\u5220\u9664\u5217\u4e4binplace\u53c2\u6570\u5b9e\u4f8b\u5c31\u662f\u5c0f\u7f16\u5206\u4eab\u7ed9\u5927\u5bb6\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003<\/p>\n","protected":false},"excerpt":{"rendered":"

drop\u65b9\u6cd5\u6709\u4e00\u4e2a\u53ef\u9009\u53c2\u6570inplace\uff0c\u8868\u660e\u53ef\u5bf9\u539f\u6570\u7ec4\u4f5c\u51fa\u4fee\u6539\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u4e0d\u7ba1\u53c2\u6570\u9ed8\u8ba4\u4e3aFalse\u8fd8\u662f […]<\/p>\n","protected":false},"author":1482,"featured_media":160637,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=195067"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195067\/revisions"}],"predecessor-version":[{"id":195179,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195067\/revisions\/195179"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160637"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=195067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=195067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=195067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}