{"id":3279,"date":"2023-07-18T08:09:04","date_gmt":"2023-07-18T08:09:04","guid":{"rendered":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/"},"modified":"2023-07-18T08:09:04","modified_gmt":"2023-07-18T08:09:04","slug":"impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/","title":{"rendered":"Come risolvere il problema: valueerror: impossibile mascherare un array non booleano contenente valori na\/nan"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un errore che potresti riscontrare quando usi i panda \u00e8:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">ValueError<\/span> : Cannot mask with non-boolean array containing NA \/ NaN values\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Questo errore si verifica in genere quando si tenta di trovare righe in un DataFrame panda che contengono una stringa particolare, ma la colonna che si sta cercando ha valori NaN.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come correggere questo errore nella pratica.<\/span><\/p>\n<h2> <strong>Come riprodurre l&#8217;errore<\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti panda DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'A', 'A', 'B', 'B'],\n                   ' <span style=\"color: #ff0000;\">position<\/span> ': ['Guard', 'Guard', np. <span style=\"color: #3366ff;\">nan<\/span> , 'Guard', 'Forward'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [22, 28, 14, 13, 19]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team position points\n0 A Guard 22\n1 A Guard 28\n2 A NaN 14\n3 B Guard 13\n4 B Forward 19<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Supponiamo ora di provare ad accedere a tutte le righe del DataFrame in cui la colonna <strong>position<\/strong> contiene la stringa \u201cGuard\u201d:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#access all rows where position column contains 'Guard'\n<\/span>df[df[' <span style=\"color: #ff0000;\">position<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (' <span style=\"color: #ff0000;\">Guard<\/span> ')]\n\n<span style=\"color: #ff0000;\">ValueError<\/span> : Cannot mask with non-boolean array containing NA \/ NaN values\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Riceviamo un errore perch\u00e9 \u00e8 presente un valore NaN nella colonna <strong>posizione<\/strong> .<\/span><\/p>\n<h2> <strong>Come correggere l&#8217;errore<\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Per evitare questo errore, usa semplicemente l&#8217;argomento <strong>na=False<\/strong> nella funzione <strong>str.contains()<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#access all rows where position column contains 'Guard', ignore NaN\n<\/span>df[df[' <span style=\"color: #ff0000;\">position<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (' <span style=\"color: #ff0000;\">Guard<\/span> ', na= <span style=\"color: #008000;\">False<\/span> )]\n\n        team position points\n0 A Guard 22\n1 A Guard 28\n3 B Guard 13\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Questa volta possiamo accedere a tutte le righe contenenti &#8220;Guard&#8221; nella colonna <strong>posizione<\/strong> senza errori.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Un altro modo per evitare questo errore \u00e8 utilizzare <strong>.fillna(False)<\/strong> come segue:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#access all rows where position column contains 'Guard', ignore NaN\n<\/span>df[df[' <span style=\"color: #ff0000;\">position<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (' <span style=\"color: #ff0000;\">Guard<\/span> '). <span style=\"color: #3366ff;\">fillna<\/span> ( <span style=\"color: #008000;\">False<\/span> )]\n\n        team position points\n0 A Guard 22\n1 A Guard 28\n3 B Guard 13<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ancora una volta possiamo accedere a tutte le righe contenenti &#8220;Guard&#8221; nella colonna <strong>posizione<\/strong> senza errori.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come correggere altri errori comuni in Python:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/errore-chiave-panda\/\" target=\"_blank\" rel=\"noopener\">Come correggere l&#8217;errore chiave nei Panda<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/valueerror-non-puo-convertire-float-nan-in-intero\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: ValueError: impossibile convertire float NaN in int<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/non-e-stato-possibile-trasmettere-gli-operandi-con-i-moduli\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: ValueError: non \u00e8 stato possibile trasmettere gli operandi con le forme<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un errore che potresti riscontrare quando usi i panda \u00e8: ValueError : Cannot mask with non-boolean array containing NA \/ NaN values Questo errore si verifica in genere quando si tenta di trovare righe in un DataFrame panda che contengono una stringa particolare, ma la colonna che si sta cercando ha valori NaN. L&#8217;esempio seguente [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T08:09:04+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\",\"url\":\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\",\"name\":\"Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-18T08:09:04+00:00\",\"dateModified\":\"2023-07-18T08:09:04+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come risolvere il problema: valueerror: impossibile mascherare un array non booleano contenente valori na\/nan\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology","description":"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/","og_locale":"it_IT","og_type":"article","og_title":"Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology","og_description":"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.","og_url":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/","og_site_name":"Statorials","article_published_time":"2023-07-18T08:09:04+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/","url":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/","name":"Come risolvere il problema: ValueError: impossibile mascherare un array non booleano contenente valori NA\/NaN - Stology","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-18T08:09:04+00:00","dateModified":"2023-07-18T08:09:04+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come correggere il seguente errore in Panda: ValueError: impossibile mascherare con un array non booleano contenente valori NA\/NaN.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come risolvere il problema: valueerror: impossibile mascherare un array non booleano contenente valori na\/nan"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3279"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=3279"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3279\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}