{"id":4672,"date":"2022-01-21T00:06:11","date_gmt":"2022-01-20T15:06:11","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=4672"},"modified":"2022-01-21T00:07:49","modified_gmt":"2022-01-20T15:07:49","slug":"%e3%80%90python%e3%80%91settingwithcopywarnig-a-value-is-trying-to-be-set-on-a-copy-of-a-slice-from-dataframe-%e3%81%8c%e5%87%ba%e3%81%9f%e6%99%82%e3%81%ae%e5%af%be%e5%87%a6%e6%b3%95","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=4672","title":{"rendered":"\u3010Python\u3011SettingWithCopyWarnig: A value is trying to be set on a copy of a slice from DataFrame \u304c\u51fa\u305f\u6642\u306e\u5bfe\u51e6\u6cd5"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#%E6%A6%82%E8%A6%81\" >\u6982\u8981<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#%E7%92%B0%E5%A2%83\" >\u74b0\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#%E7%99%BA%E7%94%9F%E7%8A%B6%E6%B3%81\" >\u767a\u751f\u72b6\u6cc1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#Warning%E3%81%AE%E8%A7%A3%E9%87%88\" >Warning\u306e\u89e3\u91c8<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#%E5%AF%BE%E5%87%A6%E6%B3%95\" >\u5bfe\u51e6\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/obenkyolab.com\/?p=4672\/#%E3%81%BE%E3%81%A8%E3%82%81\" >\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python\u306ePandas\u3067DataFrame\u3092\u64cd\u4f5c\u3057\u3066\u3044\u305f\u969b\u306b\u3001\u4ee5\u4e0b\u306e\u30a8\u30e9\u30fc\u306b\u906d\u9047\u3057\u305f\u306e\u3067\u305d\u306e\u5bfe\u51e6\u307b\u3046\u306b\u3064\u3044\u3066\u30e1\u30e2\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>SettingWithCopyWarning:\nA value is trying to be set on a copy of a slice from a DataFrame.<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%92%B0%E5%A2%83\"><\/span>\u74b0\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python 3.8.10<\/li><li>pandas 1.3.5<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%99%BA%E7%94%9F%E7%8A%B6%E6%B3%81\"><\/span>\u767a\u751f\u72b6\u6cc1<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u307e\u305a\u6b21\u306e\u3088\u3046\u306aDataFrame\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import pandas as pd\ndf = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9,]], columns=[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;])\ndf.head()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full waku\"><img loading=\"lazy\" decoding=\"async\" width=\"146\" height=\"116\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/01\/image.png\" alt=\"\" class=\"wp-image-4674\"\/><\/figure>\n\n\n\n<p>\u4e0a\u8a18\u3067\u4f5c\u6210\u3057\u305fDataFrame\u304b\u3089\u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u306a\u3069\u3067\u62bd\u51fa\u3057\u305f\u3082\u306e\u3092\u5909\u6570\u3078\u683c\u7d0d\u3057\u3001\u3055\u3089\u306b\u305d\u306e\u5909\u6570\uff08DataFrame\uff09\u306b\u5217\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3068\u3057\u305f\u3068\u3053\u308dSettingWithCopyWarnig\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df_tmp = df\ndf_tmp[&#39;col4&#39;] = df[&#39;col1&#39;]\ndf_tmp = df[[&#39;col1&#39;,&#39;col2&#39;]]\ndf_tmp[&#39;col5&#39;] = df[&#39;col2&#39;]\n----\nA value is trying to be set on a copy of a slice from a DataFrame.\nTry using .loc[row_indexer,col_indexer] = value instead\n\nSee the caveats in the documentation: https:\/\/pandas.pydata.org\/pandas-docs\/stable\/user_guide\/indexing.html#returning-a-view-versus-a-copy\n  df_tmp[&#39;col4&#39;] = df[&#39;col1&#39;]<\/code><\/pre><\/div>\n\n\n\n<p>Warning\u306f\u3001\u300c\u5024\u304c\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u30b9\u30e9\u30a4\u30b9\u306e\u30b3\u30d4\u30fc\u304c\u4ee3\u5165\u3055\u308c\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u3002\u4ee3\u308f\u308a\u306b.loc\u30e1\u30bd\u30c3\u30c9\u3092\u8a66\u3057\u3066\u304f\u3060\u3055\u3044\u300d\u3068\u3044\u3063\u305f\u5185\u5bb9\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Warning%E3%81%AE%E8%A7%A3%E9%87%88\"><\/span>Warning\u306e\u89e3\u91c8<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u9014\u4e2d\u306bdisplay(df)\u3092\u631f\u3093\u3067df\u306e\u6319\u52d5\u3092\u78ba\u8a8d\u3057\u3066\u307f\u305f\u3068\u3053\u308d\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df_tmp = df\ndf_tmp[&#39;col4&#39;] = df[&#39;col1&#39;]\ndisplay(df) # ...(1)\ndf_tmp = df[[&#39;col1&#39;,&#39;col2&#39;]]\ndisplay(df) # ...(2)\ndf_tmp[&#39;col5&#39;] = df[&#39;col2&#39;] \ndisplay(df) # ...(3)<\/code><\/pre><\/div>\n\n\n\n<p>(1), (2), (3)\u306e\u7d50\u679c\u306f\u3044\u305a\u308c\u3082\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/obenkyolab.com\/wp-content\/uploads\/2022\/01\/image-1.png\" alt=\"\" class=\"wp-image-4677\" width=\"204\" height=\"136\"\/><\/figure>\n\n\n\n<p>\u3053\u306e\u6319\u52d5\u304b\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u89e3\u91c8\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df_tmp = df\ndf_tmp[&#39;col4&#39;] = df[&#39;col1&#39;] #\u53c2\u7167\u6e21\u3057\u306b\u306a\u3063\u3066\u3044\u308b\ndf_tmp = df[[&#39;col1&#39;,&#39;col2&#39;]] #df_tmp\u3078\u30b3\u30d4\u30fc\u304c\u6e21\u3055\u308c\u3066\u3044\u308b\uff08df\u306e\u5024\u306f\u5909\u308f\u3089\u306a\u3044\uff09\ndf_tmp[&#39;col5&#39;] = df[&#39;col2&#39;] #\u30b3\u30d4\u30fc\u306b\u5bfe\u3059\u308b\u4ee3\u5165\u306a\u306e\u3067SettingWithCopyWarning\u304c\u51fa\u308b<\/code><\/pre><\/div>\n\n\n\n<p>\u3069\u3046\u3084\u3089\u3001df\u3092\u305d\u306e\u307e\u307e = \u3067\u6e21\u3057\u305f\u6642\u306b\u306f\u53c2\u7167\u6e21\u3057\u306b\u306a\u308b\u304c\u3001\u5217\u6307\u5b9a\u3067\u30b9\u30e9\u30a4\u30b9\u3057\u3066\u6e21\u3057\u305f\u5834\u5408\u306b\u306f\u30b3\u30d4\u30fc\u304c\u6e21\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3057\u3066\u3001\u30b3\u30d4\u30fc\u5148\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u5bfe\u3057\u3066\u5143\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u30b9\u30e9\u30a4\u30b9\u3092\u4ee3\u5165\u3057\u3088\u3046\u3068\u3057\u305f\u5834\u5408\u306bPython\u304c\u6c17\u3092\u5229\u304b\u305b\u3066Warning\u3057\u3066\u3044\u308b\u69d8\u5b50\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AF%BE%E5%87%A6%E6%B3%95\"><\/span>\u5bfe\u51e6\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6b21\u306e\u3088\u3046\u306b\u30b3\u30d4\u30fc\u3067\u6e21\u3059\u7b87\u6240\u306b\u3064\u3044\u3066\u306f\u660e\u793a\u7684\u306b.copy()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068 SettingWithCopyWarnig \u306f\u51fa\u306a\u304f\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df_tmp = df\ndf_tmp[&#39;col4&#39;] = df[&#39;col1&#39;]\ndf_tmp = df[[&#39;col1&#39;,&#39;col2&#39;]].copy()\ndf_tmp[&#39;col5&#39;] = df[&#39;col2&#39;]<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306f\u53c2\u7167\u6e21\u3057\u304b\u30b3\u30d4\u30fc\u6e21\u3057\u304b\u304c\u5206\u304b\u308a\u306b\u304f\u3044\u305f\u3081S SettingWithCopyWarnig \u304c\u51fa\u305f\u5834\u5408\u306f\u306a\u308b\u3079\u304f\u660e\u793a\u7684\u306bcopy()\u3092\u4f7f\u7528\u3057\u3066\u3084\u308b\u306e\u304c\u826f\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Python\u306ePandas\u3067DataFrame\u3092\u64cd\u4f5c\u3057\u3066\u3044\u305f\u969b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=4672","footnotes":""},"categories":[7],"tags":[],"class_list":["post-4672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4672"}],"version-history":[{"count":5,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4672\/revisions"}],"predecessor-version":[{"id":4679,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/4672\/revisions\/4679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}