{"id":3726,"date":"2023-07-15T22:05:44","date_gmt":"2023-07-15T22:05:44","guid":{"rendered":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/"},"modified":"2023-07-15T22:05:44","modified_gmt":"2023-07-15T22:05:44","slug":"pandas-verifica-se-a-string-contem-varias-substrings","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/","title":{"rendered":"Pandas: verifique se a string cont\u00e9m v\u00e1rias substrings"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar os seguintes m\u00e9todos para verificar se uma string em um DataFrame do pandas cont\u00e9m v\u00e1rias substrings:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>M\u00e9todo 1: verifique se a string cont\u00e9m uma das v\u00e1rias substrings<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">string_column<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (' <span style=\"color: #ff0000;\">|<\/span> ' <span style=\"color: #3366ff;\">.join<\/span> ([' <span style=\"color: #ff0000;\">string1<\/span> ',' <span style=\"color: #ff0000;\">string2<\/span> ']))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>M\u00e9todo 2: verifique se a string cont\u00e9m v\u00e1rias substrings<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">string_column<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> ( <span style=\"color: #ff0000;\">r'^(?=.*string1)(?=.*string2)'<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Os exemplos a seguir mostram como usar cada m\u00e9todo na pr\u00e1tica com o seguinte DataFrame do pandas:<\/span><\/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\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['Good East Team', 'Good West Team', 'Great East Team',\n                             'Great West Team', 'Bad East Team', 'Bad West Team'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [93, 99, 105, 110, 85, 88]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n              team points\n0 Good East Team 93\n1 Good West Team 99\n2 Great East Team 105\n3 Great West Team 110\n4 Bad East Team 85\n5 Bad West Team 88\n<\/strong><\/span><\/pre>\n<h2> <strong>Exemplo<\/strong> <strong>1: <span style=\"color: #000000;\">Verifique se a string cont\u00e9m uma das v\u00e1rias substrings<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Podemos usar a seguinte sintaxe para verificar se cada string na coluna <strong>da equipe<\/strong> cont\u00e9m a substring &#8220;Bom&#8221; <strong>ou<\/strong> &#8220;\u00c9&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#create new column that checks if each team name contains 'Good' or 'East'\n<\/span>df[' <span style=\"color: #ff0000;\">good_or_east<\/span> '] = df[' <span style=\"color: #ff0000;\">team<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> (' <span style=\"color: #ff0000;\">|<\/span> ' <span style=\"color: #3366ff;\">.join<\/span> ([' <span style=\"color: #ff0000;\">Good<\/span> ',' <span style=\"color: #ff0000;\">East<\/span> ']))\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n              team points good_or_east\n0 Good East Team 93 True\n1 Good West Team 99 True\n2 Great East Team 105 True\n3 Great West Team 110 False\n4 Bad East Team 85 True\n5 Bad West Team 88 False<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">A nova coluna <strong>good_or_east<\/strong> retorna os seguintes valores:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>Verdadeiro<\/strong> se a equipe contiver \u201cBom\u201d ou \u201c\u00c9\u201d<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Falso<\/strong> se a equipe n\u00e3o cont\u00e9m nem \u201cBom\u201d nem \u201c\u00c9\u201d<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : O <strong>|<\/strong> O operador significa \u201cou\u201d em pandas.<\/span><\/p>\n<h2> <strong>Exemplo 2<\/strong> <strong>: <span style=\"color: #000000;\">Verifique se a string cont\u00e9m v\u00e1rias substrings<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Podemos usar a seguinte sintaxe para verificar se cada string na coluna <strong>da equipe<\/strong> cont\u00e9m as substring \u201cBom\u201d <b>e<\/b> \u201cIs\u201d:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#create new column that checks if each team name contains 'Good' and 'East'<\/span>\ndf[' <span style=\"color: #ff0000;\">good_and_east<\/span> '] = df[' <span style=\"color: #ff0000;\">team<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">contains<\/span> ( <span style=\"color: #ff0000;\">r'^(?=.*Good)(?=.*East)'<\/span> )\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n              team points good_and_east\n0 Good East Team 93 True\n1 Good West Team 99 False\n2 Great East Team 105 False\n3 Great West Team 110 False\n4 Bad East Team 85 False\n5 Bad West Team 88 False<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">A nova coluna <strong>good_and_east<\/strong> retorna os seguintes valores:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>Verdadeiro<\/strong> se a equipe contiver \u201cBom\u201d e \u201c\u00c9\u201d<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Falso<\/strong> se a equipe n\u00e3o contiver \u201cBom\u201d e \u201c\u00c9\u201d<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Observe que apenas um valor <strong>True<\/strong> \u00e9 retornado, pois h\u00e1 apenas um nome de equipe contendo a substring &#8220;Boa&#8221; <strong>e<\/strong> a substring &#8220;Leste&#8221;.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como realizar outras tarefas comuns em pandas:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/pandas-adiciona-uma-coluna-de-um-dataframe-para-outro\/\" target=\"_blank\" rel=\"noopener\">Pandas: adicione uma coluna de um DataFrame para outro<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/pandas-obtem-linhas-que-nao-estao-em-outro-dataframe\/\" target=\"_blank\" rel=\"noopener\">Pandas: Obtenha linhas que n\u00e3o est\u00e3o em outro DataFrame<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/pandas-verifica-se-multiplas-colunas-sao-iguais\/\" target=\"_blank\" rel=\"noopener\">Pandas: como verificar se v\u00e1rias colunas s\u00e3o iguais<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar os seguintes m\u00e9todos para verificar se uma string em um DataFrame do pandas cont\u00e9m v\u00e1rias substrings: M\u00e9todo 1: verifique se a string cont\u00e9m uma das v\u00e1rias substrings df[&#8216; string_column &#8216;]. str . contains (&#8216; | &#8216; .join ([&#8216; string1 &#8216;,&#8217; string2 &#8216;])) M\u00e9todo 2: verifique se a string cont\u00e9m v\u00e1rias substrings df[&#8216; [&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":[],"class_list":["post-3726","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.\" \/>\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\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T22:05:44+00:00\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/\",\"url\":\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/\",\"name\":\"Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-15T22:05:44+00:00\",\"dateModified\":\"2023-07-15T22:05:44+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pandas: verifique se a string cont\u00e9m v\u00e1rias substrings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia","description":"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.","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\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/","og_locale":"pt_PT","og_type":"article","og_title":"Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia","og_description":"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.","og_url":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/","og_site_name":"Statorials","article_published_time":"2023-07-15T22:05:44+00:00","author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/","url":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/","name":"Pandas: Verifique se a string cont\u00e9m m\u00faltiplas substrings \u2013 Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-15T22:05:44+00:00","dateModified":"2023-07-15T22:05:44+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como verificar se strings em um DataFrame do pandas cont\u00eam m\u00faltiplas substrings, incluindo exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/pandas-verifica-se-a-string-contem-varias-substrings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Pandas: verifique se a string cont\u00e9m v\u00e1rias substrings"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=3726"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3726\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}