{"id":50619,"date":"2024-12-03T10:03:10","date_gmt":"2024-12-03T02:03:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50619\/"},"modified":"2024-12-03T10:03:10","modified_gmt":"2024-12-03T02:03:10","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e7%9a%84-pandas-%e5%ba%93%ef%bc%8c%e5%9c%a8-dataframe-%e4%b8%ad%e5%b0%86%e4%b8%80%e5%88%97%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%89%8d%e5%90%8e%e6%b7%bb%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50619\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/1732412016674282707bdb9.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4fee\u6539 dataframe \u4e2d\u4e00\u5217\u7684\u503c<\/strong><\/p>\n<p>\u5982\u4f55\u5bf9 dataframe \u4e2d\u7684\u4e00\u5217\u8fdb\u884c\u4fee\u6539\uff0c\u4f7f\u5176\u6bcf\u4e2a\u5b57\u7b26\u4e32\u524d\u540e\u90fd\u52a0\u4e0a\u7279\u5b9a\u503c\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>import pandas as pd\n\n# \u521b\u5efa DataFrame\ndf = pd.DataFrame({'a': ['cm1', 'cm2', 'cm3'], 'b': ['b1-name xxxx', 'name yyyy', 'b3-name name zzzz']})\n\n# \u83b7\u53d6\u8981\u4fee\u6539\u7684\u5185\u5bb9\ns_list = ['cm1', 'cm2', 'cm3']\ns_val = df['b'].values\n\n# \u4fee\u6539\u5185\u5bb9\u5e76\u751f\u6210\u65b0\u5b57\u7b26\u4e32\nfor i in range(len(s_list)):\n    for j in range(len(s_val)):\n        str = '!TAB!' + ' '.join(s_val[j].split()).replace(' ','!TAB!') + '!TAB!'\n        df['b'][j] = str\n\n# \u67e5\u627e\u7279\u5b9a\u5185\u5bb9\u7684\u7d22\u5f15\ns_idx = df[df['b'].str.contains('!TAB!' + s_list[0] + '!TAB!') == True].index.values\n\n# \u8f93\u51fa\u4fee\u6539\u540e\u7684 DataFrame\nprint(df)\nprint(s_idx)<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u7684 Pandas \u5e93\uff0c\u5728 DataFrame \u4e2d\u5c06\u4e00\u5217\u5b57\u7b26\u4e32\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u503c\uff0c\u5e76\u67e5\u627e\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff1f \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u4fee\u6539 dataframe \u4e2d\u4e00\u5217\u7684\u503c \u5982\u4f55\u5bf9 dataframe \u4e2d\u7684\u4e00\u5217\u8fdb\u884c\u4fee\u6539\uff0c\u4f7f\u5176\u6bcf\u4e2a\u5b57\u7b26\u4e32\u524d\u540e\u90fd\u52a0\u4e0a\u7279\u5b9a\u503c\uff1f \u89e3\u51b3\u65b9\u6848 \u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a import pandas as pd # \u521b\u5efa DataFrame df = pd.DataFrame({&#8216;a&#8217;: [&#8216;cm1&#8217;, &#8216;cm2&#8217;, &#8216;cm3&#8217;], &#8216;b&#8217;: [&#8216;b1-name xxxx&#8217;, &#8216;name yyyy&#8217;, &#8216;b3-name name zzzz&#8217;]}) # \u83b7\u53d6\u8981\u4fee\u6539\u7684\u5185\u5bb9 s_list = [&#8216;cm1&#8217;, &#8216;cm2&#8217;, &#8216;cm3&#8217;] s_val = df[&#8216;b&#8217;].values # \u4fee\u6539\u5185\u5bb9\u5e76\u751f\u6210\u65b0\u5b57\u7b26\u4e32 for i in range(len(s_list)): for j [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50619","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50619"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50619\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}