{"id":3860,"date":"2023-07-15T03:19:01","date_gmt":"2023-07-15T03:19:01","guid":{"rendered":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/"},"modified":"2023-07-15T03:19:01","modified_gmt":"2023-07-15T03:19:01","slug":"cerca-la-stringa-nel-dataframe-panda","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/","title":{"rendered":"Panda: cerca una stringa in tutte le colonne di dataframe"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi per cercare una stringa particolare in ciascuna colonna di un DataFrame panda e filtrare le righe contenenti la stringa in almeno una colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define filter\n<\/span><span style=\"color: #000000;\">mask = np. <span style=\"color: #3366ff;\">column_stack<\/span> ([df[col]. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (r \" <span style=\"color: #ff0000;\">my_string<\/span> \", na= <span style=\"color: #008000;\">False<\/span> ) <span style=\"color: #008000;\">for<\/span> col <span style=\"color: #008000;\">in<\/span> df])\n<\/span>\n<span style=\"color: #008080;\">#filter for rows where any column contains 'my_string'\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [mask. <span style=\"color: #3366ff;\">any<\/span> (axis= <span style=\"color: #008000;\">1<\/span> )]<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: cerca una stringa in tutte le colonne di Pandas DataFrame<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente DataFrame panda che contiene informazioni sul primo e sul secondo ruolo di vari giocatori di basket in una squadra:<\/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 style=\"color: #000000;\">df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">player<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n                   ' <span style=\"color: #ff0000;\">first_role<\/span> ': ['P Guard', 'P Guard', 'S Guard', 'S Forward',\n                                  'P Forward', 'Center', 'Center', 'Center'],\n                   ' <span style=\"color: #ff0000;\">second_role<\/span> ': ['S Guard', 'S Guard', 'Forward', 'S Guard',\n                                   'S Guard', 'S Forward', 'P Forward', 'P Forward']})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  player first_role second_role\n0 AP Guard S Guard\n1 BP Guard S Guard\n2 CS Guard Forward\n3DS Forward S Guard\n4 EP Forward S Guard\n5 F Center S Forward\n6 G Center P Forward\n7 H Center P Forward<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come filtrare il DataFrame panda per le righe in cui appare la stringa &#8220;Guard&#8221; in qualsiasi colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#define filter\n<\/span>mask = np. <span style=\"color: #3366ff;\">column_stack<\/span> ([df[col]. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (r \" <span style=\"color: #ff0000;\">Guard<\/span> \", na= <span style=\"color: #008000;\">False<\/span> ) <span style=\"color: #008000;\">for<\/span> col <span style=\"color: #008000;\">in<\/span> df])\n\n<span style=\"color: #008080;\">#filter for rows where any column contains 'Guard'\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [mask. <span style=\"color: #3366ff;\">any<\/span> (axis= <span style=\"color: #008000;\">1<\/span> )]\n\n        player first_role second_role\n0 A P Guard S Guard\n1 B P Guard S Guard\n2 C S Guard Forward\n3 D S Forward S Guard\n4 E P Forward S Guard\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che ogni riga del DataFrame risultante contiene la stringa &#8220;Guard&#8221; in almeno una colonna.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Puoi anche filtrare le righe in cui una delle numerose stringhe appare in almeno una colonna utilizzando l&#8217;operatore &#8220;OR&#8221; ( <strong>|<\/strong> ) in panda.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, il codice seguente mostra come filtrare le righe in cui appare &#8220;P Guard&#8221; o &#8220;Center&#8221; in almeno una colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#define filter\n<\/span>mask = np. <span style=\"color: #3366ff;\">column_stack<\/span> ([df[col]. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (r \" <span style=\"color: #ff0000;\">P Guard|Center<\/span> \", na= <span style=\"color: #008000;\">False<\/span> ) <span style=\"color: #008000;\">for<\/span> col <span style=\"color: #008000;\">in<\/span> df])\n\n<span style=\"color: #008080;\">#filter for rows where any column contains 'P Guard' or 'Center'\n<\/span>df. <span style=\"color: #3366ff;\">loc<\/span> [mask. <span style=\"color: #3366ff;\">any<\/span> (axis= <span style=\"color: #008000;\">1<\/span> )]\n\n        player first_role second_role\n0 A P Guard S Guard\n1 B P Guard S Guard\n5 F Center S Forward\n6 G Center P Forward\n7 H Center P Forward\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che ogni riga nel DataFrame risultante contiene &#8220;P Guard&#8221; o &#8220;Center&#8221; in almeno una colonna.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : \u00e8 importante includere l&#8217;argomento <strong>na=False<\/strong> nella funzione <strong>contiene()<\/strong> altrimenti riscontrerai <a href=\"https:\/\/statorials.org\/it\/impossibile-mascherare-con-un-array-non-booleano-contenente-valori-na-nan\/\" target=\"_blank\" rel=\"noopener\">un errore<\/a> se i valori NaN sono presenti nel DataFrame.<\/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 operazioni di filtro comuni nei panda:<\/span><\/p>\n<p> Come filtrare un Pandas DataFrame in base ai valori delle colonne<br \/> Come filtrare le righe Pandas DataFrame per data<br \/><a href=\"https:\/\/statorials.org\/it\/i-panda-filtrano-piu-condizioni\/\" target=\"_blank\" rel=\"noopener\">Come filtrare un Pandas DataFrame su pi\u00f9 condizioni<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi per cercare una stringa particolare in ciascuna colonna di un DataFrame panda e filtrare le righe contenenti la stringa in almeno una colonna: #define filter mask = np. column_stack ([df[col]. str . contains (r &#8221; my_string &#8220;, na= False ) for col in df]) #filter for rows where any [&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>Panda: trova una stringa in tutte le colonne di DataFrame - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.\" \/>\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\/cerca-la-stringa-nel-dataframe-panda\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: trova una stringa in tutte le colonne di DataFrame - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T03:19:01+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\/cerca-la-stringa-nel-dataframe-panda\/\",\"url\":\"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/\",\"name\":\"Panda: trova una stringa in tutte le colonne di DataFrame - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-15T03:19:01+00:00\",\"dateModified\":\"2023-07-15T03:19:01+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: cerca una stringa in tutte le colonne di dataframe\"}]},{\"@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":"Panda: trova una stringa in tutte le colonne di DataFrame - Statorials","description":"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.","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\/cerca-la-stringa-nel-dataframe-panda\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: trova una stringa in tutte le colonne di DataFrame - Statorials","og_description":"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.","og_url":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/","og_site_name":"Statorials","article_published_time":"2023-07-15T03:19:01+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\/cerca-la-stringa-nel-dataframe-panda\/","url":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/","name":"Panda: trova una stringa in tutte le colonne di DataFrame - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-15T03:19:01+00:00","dateModified":"2023-07-15T03:19:01+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come cercare una stringa particolare in ciascuna colonna di un DataFrame panda, incluso un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/cerca-la-stringa-nel-dataframe-panda\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: cerca una stringa in tutte le colonne di dataframe"}]},{"@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\/3860"}],"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=3860"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3860\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}