{"id":4249,"date":"2023-07-12T13:22:19","date_gmt":"2023-07-12T13:22:19","guid":{"rendered":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/"},"modified":"2023-07-12T13:22:19","modified_gmt":"2023-07-12T13:22:19","slug":"filtro-panda-per-colonna-booleana","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/","title":{"rendered":"Come filtrare pandas dataframe utilizzando colonne booleane"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare i seguenti metodi per filtrare le righe in un DataFrame panda in base ai valori booleani delle colonne:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: filtra DataFrame in base a una colonna booleana<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where value in 'my_column' is True\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [df. <span style=\"color: #3366ff;\">my_column<\/span> ]\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: filtra DataFrame in base a pi\u00f9 colonne booleane<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where value in 'column1' <em>or<\/em> 'column2' is True\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [df. <span style=\"color: #3366ff;\">column1<\/span> <span style=\"color: #800080;\">|<\/span> df. <span style=\"color: #3366ff;\">column2<\/span> ]<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare ciascun metodo nella pratica con i seguenti DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [18,20, 25, 40, 34, 32, 19],\n                   ' <span style=\"color: #ff0000;\">all_star<\/span> ': [True, False, True, True, True, False, False],\n                   ' <span style=\"color: #ff0000;\">starter<\/span> ': [False, True, True, True, False, False, False]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points all_star starter\n0 A 18 True False\n1 B 20 False True\n2 C 25 True True\n3 D 40 True True\n4 E 34 True False\n5 F 32 False False\n6 G 19 False False\n<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: filtra il DataFrame in base a una colonna booleana<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per filtrare il DataFrame panda in modo che contenga solo righe in cui il valore nella colonna <strong>all_star<\/strong> \u00e8 True:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where 'all_star' is True\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [df. <span style=\"color: #3366ff;\">all_star<\/span> ]\n\n\tteam points all_star starter\n0 A 18 True False\n2 C 25 True True\n3 D 40 True True\n4 E 34 True False\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che DataFrame \u00e8 stato filtrato per contenere solo righe il cui valore nella colonna <strong>all_star<\/strong> \u00e8 True.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se invece vuoi filtrare le righe in cui <strong>all_star<\/strong> \u00e8 False, digita semplicemente una tilde ( <strong>~<\/strong> ) davanti al nome della colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where 'all_star' is False\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [ <span style=\"color: #800080;\">~<\/span> df. <span style=\"color: #3366ff;\">all_star<\/span> ]\n\n        team points all_star starter\n1 B 20 False True\n5 F 32 False False\n6 G 19 False False\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il DataFrame \u00e8 stato ora filtrato per contenere solo righe il cui valore nella colonna <strong>all_star<\/strong> \u00e8 False.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: filtra il DataFrame in base a pi\u00f9 colonne booleane<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per filtrare il DataFrame panda in modo che contenga solo righe in cui il valore della colonna <strong>all_star<\/strong> <em>o<\/em> della colonna <strong>iniziale<\/strong> \u00e8 True:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where 'all_star' <em>or<\/em> 'starter' is True\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [df. <span style=\"color: #3366ff;\">all_star<\/span> <span style=\"color: #800080;\">|<\/span> df. <span style=\"color: #3366ff;\">starter<\/span> ]\n\n        team points all_star starter\n0 A 18 True False\n1 B 20 False True\n2 C 25 True True\n3 D 40 True True\n4 E 34 True False\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che DataFrame \u00e8 stato filtrato per contenere solo righe il cui valore nella colonna <strong>all_star<\/strong> <em>o<\/em> <strong>starter<\/strong> \u00e8 True.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se desideri filtrare le righe il cui valore <em>nelle<\/em> colonne <strong>all_star<\/strong> e <strong>starter<\/strong> \u00e8 True, puoi utilizzare l&#8217;operatore <strong>&amp;<\/strong> invece di <strong>|<\/strong> operatore:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#filter for rows where 'all_star' <i>and<\/i> 'starter' is True\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [df. <span style=\"color: #3366ff;\">all_star<\/span> <span style=\"color: #800080;\">&amp;<\/span> df. <span style=\"color: #3366ff;\">starter<\/span> ]\n\n\tteam points all_star starter\n2 C 25 True True\n3 D 40 True True\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ora il DataFrame \u00e8 stato filtrato per contenere solo le righe in cui il valore nelle colonne <strong>all_star<\/strong> <i>e<\/i> <strong>starter<\/strong> \u00e8 True.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Correlati:<\/strong> <a href=\"https:\/\/statorials.org\/it\/panda-loc-contro-iloc\/\" target=\"_blank\" rel=\"noopener\">la differenza tra loc e iloc su Pandas<\/a><\/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 eseguire altre attivit\u00e0 comuni nei panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/i-panda-selezionano-le-righe-in-base-alla-serie-booleana\/\" target=\"_blank\" rel=\"noopener\">Panda: seleziona le righe da DataFrame utilizzando una serie booleana<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-creano-colonne-booleane-in-base-alla-condizione\/\" target=\"_blank\" rel=\"noopener\">Panda: come creare una colonna booleana basata sulla condizione<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-convertono-booleani-in-interi\/\" target=\"_blank\" rel=\"noopener\">Panda: come convertire valori booleani in valori interi<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare i seguenti metodi per filtrare le righe in un DataFrame panda in base ai valori booleani delle colonne: Metodo 1: filtra DataFrame in base a una colonna booleana #filter for rows where value in &#8216;my_column&#8217; is True df. loc [df. my_column ] Metodo 2: filtra DataFrame in base a pi\u00f9 colonne booleane [&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 filtrare Pandas DataFrame utilizzando colonne booleane - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.\" \/>\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\/filtro-panda-per-colonna-booleana\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come filtrare Pandas DataFrame utilizzando colonne booleane - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T13:22:19+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=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/\",\"url\":\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/\",\"name\":\"Come filtrare Pandas DataFrame utilizzando colonne booleane - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-12T13:22:19+00:00\",\"dateModified\":\"2023-07-12T13:22:19+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come filtrare pandas dataframe utilizzando colonne booleane\"}]},{\"@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 filtrare Pandas DataFrame utilizzando colonne booleane - Statorials","description":"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.","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\/filtro-panda-per-colonna-booleana\/","og_locale":"it_IT","og_type":"article","og_title":"Come filtrare Pandas DataFrame utilizzando colonne booleane - Statorials","og_description":"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.","og_url":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/","og_site_name":"Statorials","article_published_time":"2023-07-12T13:22:19+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/","url":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/","name":"Come filtrare Pandas DataFrame utilizzando colonne booleane - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-12T13:22:19+00:00","dateModified":"2023-07-12T13:22:19+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come filtrare le righe di un DataFrame panda in base ai valori delle colonne booleane, con esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/filtro-panda-per-colonna-booleana\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come filtrare pandas dataframe utilizzando colonne booleane"}]},{"@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\/4249"}],"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=4249"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4249\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}