{"id":3950,"date":"2023-07-14T13:59:17","date_gmt":"2023-07-14T13:59:17","guid":{"rendered":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/"},"modified":"2023-07-14T13:59:17","modified_gmt":"2023-07-14T13:59:17","slug":"panda-value_counts-percentuale","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/","title":{"rendered":"Panda: come rappresentare value_counts in percentuale"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la funzione <strong>value_counts()<\/strong> nei panda per contare le occorrenze di valori in una determinata colonna di un DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per rappresentare i valori come percentuali, \u00e8 possibile utilizzare uno dei seguenti metodi:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: rappresentare i valori come percentuali (formattati come decimali)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">my_col<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: rappresentare i valori come percentuali (formattati con simboli percentuali)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">my_col<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> ). <span style=\"color: #3366ff;\">mul<\/span> ( <span style=\"color: #008000;\">100<\/span> ). <span style=\"color: #3366ff;\">round<\/span> ( <span style=\"color: #008000;\">1<\/span> ). <span style=\"color: #3366ff;\">astype<\/span> (str) + ' <span style=\"color: #ff0000;\">%<\/span> '<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 3: rappresentare i conteggi dei valori come percentuali (con conteggi)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>counts = df. <span style=\"color: #3366ff;\">my_col<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> ()<\/strong>\n<strong>percs = df. <span style=\"color: #3366ff;\">my_col<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> )<\/strong>\n<strong>p.d. <span style=\"color: #3366ff;\">concat<\/span> ([counts, percs], axis= <span style=\"color: #008000;\">1<\/span> , keys=[' <span style=\"color: #ff0000;\">count<\/span> ', ' <span style=\"color: #ff0000;\">percentage<\/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<span style=\"color: #000000;\">\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'A', 'B', 'B', 'B', 'B', 'B', 'C'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [15, 12, 18, 20, 22, 28, 35, 40]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points\n0 to 15\n1 to 12\n2 B 18\n3 B 20\n4 B 22\n5 B 28\n6 B 35\n7 C 40\n<\/span><\/span><\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: rappresentare i valori come percentuali (formattati come decimali)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare le occorrenze di ciascun valore nella colonna <strong>della squadra<\/strong> e rappresentare le occorrenze come percentuale del totale, in formato decimale:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#count occurrence of each value in 'team' column as percentage of total\n<\/span>df. <span style=\"color: #3366ff;\">team<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> )\n\nB 0.625\nAt 0.250\nC 0.125\nName: team, dtype: float64\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Il valore <strong>B<\/strong> rappresenta il 62,5% delle occorrenze nella colonna squadra.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore <strong>A<\/strong> rappresenta il 25% delle occorrenze nella colonna della squadra.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore <strong>C<\/strong> rappresenta il 12,5% delle occorrenze nella colonna squadra.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Tieni presente che le percentuali sono formattate come decimali.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: rappresentare i valori come percentuali (formattati con simboli percentuali)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare le occorrenze di ciascun valore nella colonna <strong>della squadra<\/strong> e rappresentare le occorrenze come percentuale del totale, formattato con simboli percentuali:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#count occurrence of each value in 'team' column as percentage of total\n<\/span>df. <span style=\"color: #3366ff;\">team<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> ). <span style=\"color: #3366ff;\">mul<\/span> ( <span style=\"color: #008000;\">100<\/span> ). <span style=\"color: #3366ff;\">round<\/span> ( <span style=\"color: #008000;\">1<\/span> ). <span style=\"color: #3366ff;\">astype<\/span> (str) + ' <span style=\"color: #ff0000;\">%<\/span> '\n\nB 62.5%\nAt 25.0%\nC 12.5%\nName: team, dtype: object\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che le percentuali sono formattate come stringhe con simboli di percentuale.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 3: rappresentazione di numeri di valori come percentuali (con numeri)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare le occorrenze di ciascun valore nella colonna <strong>del team<\/strong> e rappresentare le occorrenze come numeri e percentuali:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#count occurrence of each value in 'team' column\n<\/span>counts = df. <span style=\"color: #3366ff;\">team<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> ()\n\n<span style=\"color: #008080;\">#count occurrence of each value in 'team' column as percentage of total<\/span>\npercs = df. <span style=\"color: #3366ff;\">team<\/span> . <span style=\"color: #3366ff;\">value_counts<\/span> (normalize= <span style=\"color: #008000;\">True<\/span> )\n\n<span style=\"color: #008080;\">#concatenate results into one DataFrame\n<\/span>p.d. <span style=\"color: #3366ff;\">concat<\/span> ([counts, percs], axis= <span style=\"color: #008000;\">1<\/span> , keys=[' <span style=\"color: #ff0000;\">count<\/span> ', ' <span style=\"color: #ff0000;\">percentage<\/span> '])\n\n        count percentage\nB 5 0.625\nAt 2 0.250\nC 1 0.125<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che la colonna <strong>conteggio<\/strong> visualizza il conteggio di ciascun valore univoco nella colonna della squadra mentre la colonna <strong>percentuale<\/strong> visualizza ciascun valore univoco come percentuale del numero totale di occorrenze.<\/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\/il-valore-della-trama-dei-panda-conta\/\" target=\"_blank\" rel=\"noopener\">Panda: come tracciare i conti di valore<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/il-valore-del-gruppo-panda-conta\/\" target=\"_blank\" rel=\"noopener\">Panda: come utilizzare GroupBy e conteggi dei valori<\/a><br \/><a href=\"https:\/\/statorials.org\/it\/istogramma-dei-panda-per-gruppo\/\" target=\"_blank\" rel=\"noopener\">Panda: come tracciare gli istogrammi per gruppo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la funzione value_counts() nei panda per contare le occorrenze di valori in una determinata colonna di un DataFrame. Per rappresentare i valori come percentuali, \u00e8 possibile utilizzare uno dei seguenti metodi: Metodo 1: rappresentare i valori come percentuali (formattati come decimali) df. my_col . value_counts (normalize= True ) Metodo 2: rappresentare i valori [&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: come rappresentare valore_conta in percentuale - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con 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\/panda-value_counts-percentuale\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: come rappresentare valore_conta in percentuale - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-14T13:59:17+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\/panda-value_counts-percentuale\/\",\"url\":\"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/\",\"name\":\"Panda: come rappresentare valore_conta in percentuale - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-14T13:59:17+00:00\",\"dateModified\":\"2023-07-14T13:59:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come rappresentare value_counts in percentuale\"}]},{\"@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: come rappresentare valore_conta in percentuale - Statorials","description":"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con 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\/panda-value_counts-percentuale\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: come rappresentare valore_conta in percentuale - Statorials","og_description":"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con un esempio.","og_url":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/","og_site_name":"Statorials","article_published_time":"2023-07-14T13:59:17+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\/panda-value_counts-percentuale\/","url":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/","name":"Panda: come rappresentare valore_conta in percentuale - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-14T13:59:17+00:00","dateModified":"2023-07-14T13:59:17+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come rappresentare i valori value_counts() nei panda come percentuale, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/panda-value_counts-percentuale\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come rappresentare value_counts in percentuale"}]},{"@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\/3950"}],"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=3950"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3950\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}