{"id":419400,"date":"2021-03-30T12:03:33","date_gmt":"2021-03-30T04:03:33","guid":{"rendered":"http:\/\/4563.org\/?p=419400"},"modified":"2021-03-30T12:03:33","modified_gmt":"2021-03-30T04:03:33","slug":"pandas-%e8%a1%8c%e7%ad%9b%e9%80%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=419400","title":{"rendered":"pandas \u884c\u7b5b\u9009"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  pandas \u884c\u7b5b\u9009               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u8bf7\u6559\u4e00\u4e0b\u5927\u5bb6 <br \/>\u5728 DataFrame \u8fdb\u884c\u884c\u7b5b\u9009\u65f6 \u4e00\u822c\u4f7f\u7528 DataFrame.loc \u65b9\u6cd5\uff0c\u6307\u5b9a\u5217\u6807\u7b7e\uff0c\u6216\u8005\u901a\u8fc7\u5217\u7684\u8868\u8fbe\u5f0f\u4f20\u5165\u4e00\u7ec4 boolean array<br \/>\u5982\u679c\u8981 DataFrame \u884c\u5217\u6bd4\u8f83\u591a\uff0c\u8be5\u5982\u4f55\u7b5b\u9009\u5305\u542b\u6307\u5b9a \u5b57\u7b26\u4e32\u7684\u884c\u5462\uff0c\u662f\u901a\u8fc7 pandas.DataFrame.iterrows()\u5bf9\u884c\u6570\u636e\u8fdb\u884c\u5386\u904d\u5339\u914d<br \/>\u8fd8\u662f\u6709\u4ec0\u4e48\u5176\u4ed6\u7684\u597d\u65b9\u6cd5\u6ca1\uff1f \u4ee5\u4e0b\u7b80\u5355\u5b9e\u4f8b \u7b5b\u9009\u5305\u542b \u5b57\u7b26&#8217;a&#8217;\u7684\u884c<br \/>df = pd.DataFrame([[&#8220;x(a)&#8221;,&#8221;(ab)&#8221;,&#8221;c&#8221;,&#8221;d&#8221;],[&#8220;m&#8221;,&#8221;2b&#8221;,&#8221;2x(3)y&#8221;,&#8221;d&#8221;],[&#8220;a&#8221;,&#8221;n&#8221;,&#8221;c&#8221;,&#8221;4(n)e&#8221;]])<\/p>\n<p>\u518d\u5c31\u662f \u4e0d\u77e5\u6709\u6ca1\u6709 pandas \u5b9a\u4f4d\u68c0\u7d22\u3001\u589e\u5220\u6539\u67e5\u7684 cookbook \u7c7b\u7684\u8d44\u6599\u53ef\u4ee5\u53c2\u8003\uff0c<br \/>\u8fd8\u6709\u4e00\u4e2a\u5c0f\u95ee\u9898 \u5c31\u662f\u5c06 DataFrame.applymap(str) \u5904\u7406\u65f6 \u9047\u5230\u6570\u503c\u578b\uff0c\u7ed3\u5c3e\u542b 0 \u7684\uff0c\u76f4\u63a5\u7ed9\u53bb\u6389\u4e86\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u8f6c\u5316\u662f\u4fdd\u7559      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5602040\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u641c\u201cpandas \u50cf\u5199 sql\u201d\u80fd\u627e\u5230\u4e00\u7bc7\u6587\u7ae0<\/p>\n<p>\u7c7b\u578b\u95ee\u9898\uff0c\u5904\u7406\u524d astype \u8f6c\u6362\u4e00\u4e0b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602041\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @imn1 <br \/>\u8c22\u8c22\u5927\u795e\u7684\u63d0\u70b9 \u90a3\u7bc7\u6587\u7ae0\u6709\u641c\u5230\u8fc7 \u770b\u8fc7\u4e00\u90e8\u5206<br \/>\u7ed3\u5408\u8fd9\u4e2a \u95ee\u9898\u6211\u60f3\u8d77\u6765\u4e86 apply()\u76f8\u5173\u51fd\u6570 \u5305\u542b\u591a\u5217\u5904\u7406\uff0c\u8fd4\u56de\u7684\u662f DataFrame\uff0c<br \/>\u53ef\u4ee5\u5c06 DataFrame.T \u5bf9\u6240\u6709\u884c\u53d8\u6362\u6210\u5217\uff0c\u8fd4\u56de\u76f8\u5173\u5217\u4e5f\u5c31\u662f\u672c\u8eab\u7684\u884c\uff0c\u53ea\u662f\u9700\u8981\u81ea\u5b9a\u4e49\u5224\u65ad\u51fd\u6570<br \/>\u4e4b\u524d\u770b\u5230\u4e86 DataFrame.query \u51fd\u6570 \u4e5f\u662f\u8fd4\u56de DataFrame \u5217\uff0c\u5c31\u662f\u5176\u4e2d\u7684 boolean expression \u611f\u89c9\u5230\u4e0d\u5230\u6548\u679c<br \/>\u518d\u6b21\u611f\u8c22 \u5927\u795e\u7684 astype \u6307\u70b9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602042\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wuwukai007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             df.loc[(df == &#8216;a&#8217;).any()]                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602043\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wuwukai007 \u8bd5\u8fc7\u4e86 \u5b8c\u5168\u4e0d\u5bf9\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602044\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : v2sir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8981\u4e0d\u4f60\u5148 df.T \u4e00\u4e0b \uff1f \u9003~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602045\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wanv1171 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             pd.Series.str.contains()<\/p>\n<p>https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.Series.str.contains.html                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602046\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wanv1171 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6ce8\u610f\u662f\u7b5b\u9009\u884c\uff0c\u90a3\u5e94\u8be5\u8fd9\u6837 df[df.apply(lambda r: r.str.contains(&#8216;b&#8217;, case=False).any(), axis=1)] <\/p>\n<p>https:\/\/stackoverflow.com\/questions\/38980514\/most-concise-way-to-select-rows-where-any-column-contains-a-string-in-pandas-dat\/43018248                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602047\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wanv1171 \u975e\u5e38\u611f\u8c22\uff0cSeries.str.contains(),\u4e0e apply lambda \u8fd9\u79cd\u65b9\u5f0f \u8fd9\u51e0\u5929\u90fd\u4e00\u76f4\u5728\u7528\uff0c\u5c31\u662f\u8fd9\u51e0\u79cd\u7ed3\u5408 any \u51fd\u6570\u6ca1\u7ec4\u5408\u4f7f\u7528\u8fc7\uff0c\u771f\u9999\u554a\uff0c\u611f\u8c22\u611f\u8c22\uff0c\u81f4\u656c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5602048\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : HelloViper <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             series.str.contains \u5440\uff0c\u4f60\u8fde\u7eed\u95ee\u7684\u95ee\u9898\u90fd\u662f str \u5217\u7684\u5185\u7f6e\u65b9\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>pandas \u884c\u7b5b\u9009 \u8cc7\u6df1\u5927\u4f6c :&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419400"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419400"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419400\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419400"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}