{"id":4371,"date":"2023-07-11T15:48:55","date_gmt":"2023-07-11T15:48:55","guid":{"rendered":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/"},"modified":"2023-07-11T15:48:55","modified_gmt":"2023-07-11T15:48:55","slug":"campione-di-panda-con-sostituzione","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/","title":{"rendered":"Panda: come campionare le righe con sostituzione"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare l&#8217;argomento <strong>replace=True<\/strong> nella funzione pandas <strong>sample()<\/strong> per campionare casualmente le righe da un DataFrame con sostituzione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#randomly select <em>n<\/em> rows with repeats allowed<\/span>\ndf. <span style=\"color: #3366ff;\">sample<\/span> (n= <span style=\"color: #008000;\">5<\/span> , replace= <span style=\"color: #008000;\">True<\/span> ) \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Utilizzando <strong>replace=True<\/strong> si consente di includere pi\u00f9 volte la stessa riga nell&#8217;esempio.<\/span><\/p>\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: esempi di righe con sostituzione in Pandas<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente DataFrame panda che contiene informazioni su vari giocatori di basket:<\/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>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [18, 22, 19, 14, 14, 11, 20, 28],\n                   ' <span style=\"color: #ff0000;\">assists<\/span> ': [5, 7, 7, 9, 12, 9, 9, 4],\n                   ' <span style=\"color: #ff0000;\">rebounds<\/span> ': [11, 8, 10, 6, 6, 5, 9, 12]})\n                   \n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points assists rebounds\n0 A 18 5 11\n1 B 22 7 8\n2 C 19 7 10\n3 D 14 9 6\n4 E 14 12 6\n5 F 11 9 5\n6 G 20 9 9\n7:28 4 12<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Supponiamo di utilizzare la funzione <strong>sample()<\/strong> per selezionare casualmente un campione di righe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#randomly select 6 rows from DataFrame (without replacement)\n<\/span>df. <span style=\"color: #3366ff;\">sample<\/span> (n= <span style=\"color: #008000;\">6<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )\n\n        team points assists rebounds\n6 G 20 9 9\n2 C 19 7 10\n1 B 22 7 8\n7:28 4 12\n3 D 14 9 6\n0 A 18 5 11\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che sei righe sono state selezionate nel DataFrame e nessuna delle righe appare pi\u00f9 volte nell&#8217;esempio.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\"><strong>Nota<\/strong> : l&#8217;argomento <strong>random_state=0<\/strong> garantisce che questo esempio sia riproducibile.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">Supponiamo ora di utilizzare l&#8217;argomento <strong>replace=True<\/strong> per selezionare un campione casuale di righe con sostituzione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#randomly select 6 rows from DataFrame (with replacement)\n<\/span>df. <span style=\"color: #3366ff;\">sample<\/span> (n= <span style=\"color: #008000;\">6<\/span> , replace= <span style=\"color: #008000;\">True<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )\n\n        team points assists rebounds\n4 E 14 12 6\n7:28 4 12\n5 F 11 9 5\n0 A 18 5 11\n3 D 14 9 6\n3 D 14 9 6\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che la linea con la squadra &#8220;D&#8221; appare pi\u00f9 volte.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Utilizzando l&#8217;argomento <strong>replace=True<\/strong> , consentiamo alla stessa riga di apparire pi\u00f9 volte nell&#8217;esempio.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Si noti inoltre che potremmo selezionare una frazione casuale del DataFrame da includere nell&#8217;esempio utilizzando l&#8217;argomento <strong>frac<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, l&#8217;esempio seguente mostra come selezionare il 75% delle righe da includere nell&#8217;esempio di sostituzione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#randomly select 75% of rows (with replacement)\n<\/span>df. <span style=\"color: #3366ff;\">sample<\/span> (frac= <span style=\"color: #008000;\">0.75<\/span> , replace= <span style=\"color: #008000;\">True<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> ) \n\n        team points assists rebounds\n4 E 14 12 6\n7:28 4 12\n5 F 11 9 5\n0 A 18 5 11\n3 D 14 9 6\n3 D 14 9 6\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che il 75% del numero di linee (6 su 8) \u00e8 stato incluso nel campione e almeno una delle linee (con la squadra &#8220;D&#8221;) \u00e8 apparsa due volte nel campione.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione pandas <strong>sample()<\/strong> <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.sample.html\" target=\"_blank\" rel=\"noopener\">qui<\/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 altri metodi di campionamento comuni in Panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/campionamento-stratificato-dei-panda\/\" target=\"_blank\" rel=\"noopener\">Come effettuare il campionamento stratificato nei panda<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/campionamento-a-grappolo-nei-panda\/\" target=\"_blank\" rel=\"noopener\">Come eseguire il campionamento dei cluster in Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare l&#8217;argomento replace=True nella funzione pandas sample() per campionare casualmente le righe da un DataFrame con sostituzione: #randomly select n rows with repeats allowed df. sample (n= 5 , replace= True ) Utilizzando replace=True si consente di includere pi\u00f9 volte la stessa riga nell&#8217;esempio. L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica. [&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 campionare righe con sostituzione \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, 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\/campione-di-panda-con-sostituzione\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: Come campionare righe con sostituzione \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, incluso un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-11T15:48:55+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\/campione-di-panda-con-sostituzione\/\",\"url\":\"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/\",\"name\":\"Panda: Come campionare righe con sostituzione \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-11T15:48:55+00:00\",\"dateModified\":\"2023-07-11T15:48:55+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, incluso un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come campionare le righe con sostituzione\"}]},{\"@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 campionare righe con sostituzione \u2013 Statorials","description":"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, 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\/campione-di-panda-con-sostituzione\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: Come campionare righe con sostituzione \u2013 Statorials","og_description":"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, incluso un esempio.","og_url":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/","og_site_name":"Statorials","article_published_time":"2023-07-11T15:48:55+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\/campione-di-panda-con-sostituzione\/","url":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/","name":"Panda: Come campionare righe con sostituzione \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-11T15:48:55+00:00","dateModified":"2023-07-11T15:48:55+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come campionare le righe da un DataFrame Panda con sostituzione, incluso un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/campione-di-panda-con-sostituzione\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come campionare le righe con sostituzione"}]},{"@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\/4371"}],"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=4371"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4371\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}