{"id":1521,"date":"2023-07-26T01:16:58","date_gmt":"2023-07-26T01:16:58","guid":{"rendered":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/"},"modified":"2023-07-26T01:16:58","modified_gmt":"2023-07-26T01:16:58","slug":"tabela-de-contingencia-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/","title":{"rendered":"Como criar uma tabela de conting\u00eancia em python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Uma <strong>tabela de conting\u00eancia<\/strong> \u00e9 um tipo de tabela que resume o relacionamento entre duas vari\u00e1veis categ\u00f3ricas.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Para criar uma tabela de conting\u00eancia em Python, podemos usar a fun\u00e7\u00e3o <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.crosstab.html\" target=\"_blank\" rel=\"noopener\">pandas.crosstab()<\/a> , que usa a seguinte sintaxe:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>pandas.crosstab(\u00edndice, colunas)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Ouro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>\u00edndice:<\/strong> nome da vari\u00e1vel a ser exibida nas linhas da tabela de conting\u00eancia<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>colunas:<\/strong> nome da vari\u00e1vel a ser exibida nas colunas da tabela de conting\u00eancia<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">O exemplo passo a passo a seguir mostra como usar esta fun\u00e7\u00e3o para criar uma tabela de conting\u00eancia em Python.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Etapa 1: crie os dados<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Primeiro, vamos criar um conjunto de dados que exiba informa\u00e7\u00f5es sobre 20 pedidos de produtos diferentes, incluindo o tipo de produto adquirido (TV, computador ou r\u00e1dio), bem como o pa\u00eds (A, B ou C) em que o produto foi comprado:<\/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;\">#create data<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({'Order': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n                            11, 12, 13, 14, 15, 16, 17, 18, 19, 20],\n                   'Product': ['TV', 'TV', 'Comp', 'TV', 'TV', 'Comp',\n                               'Comp', 'Comp', 'TV', 'Radio', 'TV', 'Radio', 'Radio',\n                               'Radio', 'Comp', 'Comp', 'TV', 'TV', 'Radio', 'TV'],\n                   'Country': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B',\n                               'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']})\n\n<span style=\"color: #008080;\">#view data\n<\/span>df\n\n        Order Product Country\n0 1 TV A\n1 2 TV A\n2 3 Comp A\n3 4 TV A\n4 5 TV B\n5 6 Comp B\n6 7 Comp B\n7 8 Comp B\n8 9 TV B\n9 10 Radio B\n10 11 TV B\n11 12 Radio B\n12 13 Radio C\n13 14 Radio C\n14 15 Comp C\n15 16 Comp C\n16 17 TV C\n17 18 TV C\n18 19 Radio C\n19 20 TV C<\/strong><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Passo 2: Crie a tabela de conting\u00eancia<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como criar uma tabela de conting\u00eancia para contar a quantidade de cada produto pedido por cada pa\u00eds:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create contingency table<\/span>\np.d. <span style=\"color: #3366ff;\">crosstab<\/span> (index=df[' <span style=\"color: #008000;\">Country<\/span> '], columns=df[' <span style=\"color: #008000;\">Product<\/span> '])\n\nProduct Comp Radio TV\nCountry\t\t\t\nA 1 0 3\nB 3 2 3\nC 2 3 3<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Veja como interpretar a tabela:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Um total de <strong>1<\/strong> computador foi adquirido no pa\u00eds A.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>3<\/strong> computadores foram adquiridos no pa\u00eds B.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>2<\/strong> computadores foram adquiridos no pa\u00eds C.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total <strong>de 0<\/strong> r\u00e1dios foram adquiridos no pa\u00eds A.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>2<\/strong> r\u00e1dios foram adquiridos no pa\u00eds B.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>3<\/strong> r\u00e1dios foram adquiridos no pa\u00eds C.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total <strong>de 3<\/strong> televisores foram adquiridos no pa\u00eds A.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total <strong>de 3<\/strong> televisores foram adquiridos no pa\u00eds B.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total <strong>de 3<\/strong> televisores foram adquiridos no pa\u00eds C.<\/span><\/li>\n<\/ul>\n<h3> <strong><span style=\"color: #000000;\">Passo 3: Adicionar totais de margem \u00e0 tabela de conting\u00eancia<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Podemos usar o argumento <strong>margins=True<\/strong> para adicionar os totais de margem \u00e0 tabela de conting\u00eancia:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#add margins to contingency table<\/span>\np.d. <span style=\"color: #3366ff;\">crosstab<\/span> (index=df[' <span style=\"color: #008000;\">Country<\/span> '], columns=df[' <span style=\"color: #008000;\">Product<\/span> '], margins= <span style=\"color: #008000;\">True<\/span> )\n\nProduct Comp Radio TV All\nCountry\t\t\t\t\nA 1 0 3 4\nB 3 2 3 8\nC 2 3 3 8\nAll 6 5 9 20<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">A forma de interpretar os valores da tabela \u00e9 a seguinte:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Totais de linha:<\/strong><\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Um total de <strong>4<\/strong> pedidos foram feitos do pa\u00eds A.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>8<\/strong> pedidos foram feitos do pa\u00eds B.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Um total de <strong>8<\/strong> pedidos foram feitos do pa\u00eds C.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><strong>Totais da coluna:<\/strong><\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Foram adquiridos um total de <strong>6<\/strong> computadores.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Foram adquiridos um total de <strong>5<\/strong> r\u00e1dios.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Foram adquiridos um total de <strong>9<\/strong> televisores.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">O valor no canto inferior direito da tabela mostra que foram encomendados um total de <strong>20<\/strong> produtos de todos os pa\u00edses.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-em-r\/\" target=\"_blank\" rel=\"noopener\">Como criar uma tabela de conting\u00eancia em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-excel\/\" target=\"_blank\" rel=\"noopener\">Como criar uma tabela de conting\u00eancia no Excel<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uma tabela de conting\u00eancia \u00e9 um tipo de tabela que resume o relacionamento entre duas vari\u00e1veis categ\u00f3ricas. Para criar uma tabela de conting\u00eancia em Python, podemos usar a fun\u00e7\u00e3o pandas.crosstab() , que usa a seguinte sintaxe: pandas.crosstab(\u00edndice, colunas) Ouro: \u00edndice: nome da vari\u00e1vel a ser exibida nas linhas da tabela de conting\u00eancia colunas: nome da [&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-1521","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>Como criar uma tabela de conting\u00eancia em Python<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.\" \/>\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\/tabela-de-contingencia-python\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como criar uma tabela de conting\u00eancia em Python\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-26T01:16:58+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=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/\",\"url\":\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/\",\"name\":\"Como criar uma tabela de conting\u00eancia em Python\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-26T01:16:58+00:00\",\"dateModified\":\"2023-07-26T01:16:58+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como criar uma tabela de conting\u00eancia em python\"}]},{\"@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":"Como criar uma tabela de conting\u00eancia em Python","description":"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.","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\/tabela-de-contingencia-python\/","og_locale":"pt_PT","og_type":"article","og_title":"Como criar uma tabela de conting\u00eancia em Python","og_description":"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.","og_url":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/","og_site_name":"Statorials","article_published_time":"2023-07-26T01:16:58+00:00","author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/","url":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/","name":"Como criar uma tabela de conting\u00eancia em Python","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-26T01:16:58+00:00","dateModified":"2023-07-26T01:16:58+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como criar uma tabela de conting\u00eancia em Python, com um exemplo passo a passo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/tabela-de-contingencia-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como criar uma tabela de conting\u00eancia em python"}]},{"@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\/1521","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=1521"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1521\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}