{"id":2355,"date":"2023-07-22T15:31:21","date_gmt":"2023-07-22T15:31:21","guid":{"rendered":"https:\/\/statorials.org\/it\/cbind-in-python\/"},"modified":"2023-07-22T15:31:21","modified_gmt":"2023-07-22T15:31:21","slug":"cbind-in-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/cbind-in-python\/","title":{"rendered":"Come usare cbind in python (equivalente a r)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">La funzione <strong>cbind<\/strong> in R, abbreviazione di <em>column-bind<\/em> , pu\u00f2 essere utilizzata per combinare insieme i frame di dati in base alle loro colonne.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo usare la funzione pandas <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.concat.html\" target=\"_blank\" rel=\"noopener\">concat()<\/a> per eseguire la funzione equivalente in Python:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df3 = pd. <span style=\"color: #3366ff;\">concat<\/span> ([df1, df2], axis= <span style=\"color: #008000;\">1<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare questa funzione nella pratica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: utilizzare cbind in Python con valori di indice uguali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti due DataFrames 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;\">#define DataFrames\n<span style=\"color: #000000;\">df1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">points<\/span> ': [99, 91, 104, 88, 108]})\n\n<span style=\"color: #008000;\">print<\/span> (df1)\n\n  team points\n0 to 99\n1 B 91\n2 C 104\n3 D 88\n4 E 108\n\ndf2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">assists<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">rebounds<\/span> ': [22, 19, 25, 33, 29]})\n\n<span style=\"color: #008000;\">print<\/span> (df2)\n\n  rebound assists\n0 to 22\n1 B 19\n2 C 25\n3 D 33\n4 E 29\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo usare la funzione <strong>concat()<\/strong> per collegare rapidamente questi due DataFrame tramite le loro colonne:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#column-bind two DataFrames into new DataFrame\n<span style=\"color: #000000;\">df3 = pd. <span style=\"color: #3366ff;\">concat<\/span> ([df1, df2], axis= <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#view resulting DataFrame<\/span>\ndf3\n\n\tteam points assists rebounds\n0 to 99 to 22\n1 B 91 B 19\n2 C 104 C 25\n3 D 88 D 33\n4 E 108 E 29<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: utilizzo di cbind in Python con valori di indice diversi<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti due DataFrames 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;\">#define DataFrames\n<span style=\"color: #000000;\">df1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">points<\/span> ': [99, 91, 104, 88, 108]})\n\n<span style=\"color: #008000;\">print<\/span> (df1)\n\n  team points\n0 to 99\n1 B 91\n2 C 104\n3 D 88\n4 E 108\n\ndf2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">assists<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">rebounds<\/span> ': [22, 19, 25, 33, 29]})\n\ndf2. <span style=\"color: #3366ff;\">index<\/span> = [6, 7, 8, 9, 10]\n\n<span style=\"color: #008000;\">print<\/span> (df2)\n\n   rebound assists\n6 to 22\n7 B 19\n8 C 25\n9 D 33\n10 E 29\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che i due DataFrame non hanno gli stessi valori di indice.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se proviamo a utilizzare la funzione <strong>concat()<\/strong> per collegarli insieme, otterremo il seguente risultato:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#attempt to column-bind two DataFrames\n<span style=\"color: #000000;\">df3 = pd. <span style=\"color: #3366ff;\">concat<\/span> ([df1, df2], axis= <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#view resulting DataFrame\n<\/span>df3\n\n\tteam points assists rebounds\n0 to 99.0 NaN NaN\n1 B 91.0 NaN NaN\n2 C 104.0 NaN NaN\n3 D 88.0 NaN NaN\n4 E 108.0 NaN NaN\n6 NaN NaN A 22.0\n7 NaN NaN B 19.0\n8 NaN NaN C 25.0\n9 NaN NaN D 33.0\n10 NaN NaN E 29.0<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo non \u00e8 il risultato che volevamo.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per risolvere questo problema, dobbiamo prima reimpostare l&#8217;indice di ciascun DataFrame prima di concatenarli insieme:<\/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;\">#define DataFrames\n<span style=\"color: #000000;\">df1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">points<\/span> ': [99, 91, 104, 88, 108]})\n\ndf2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">assists<\/span> ': ['A', 'B', 'C', 'D', 'E'],\n                    ' <span style=\"color: #ff0000;\">rebounds<\/span> ': [22, 19, 25, 33, 29]})\n\ndf2. <span style=\"color: #3366ff;\">index<\/span> = [6, 7, 8, 9, 10]\n\n<span style=\"color: #008080;\">#reset index of each DataFrame\n<\/span>df1. <span style=\"color: #3366ff;\">reset_index<\/span> (drop= <span style=\"color: #008000;\">True<\/span> , place= <span style=\"color: #008000;\">True<\/span> )\ndf2. <span style=\"color: #3366ff;\">reset_index<\/span> (drop= <span style=\"color: #008000;\">True<\/span> , place= <span style=\"color: #008000;\">True<\/span> )\n\n<span style=\"color: #008080;\">#column-bind two DataFrames\n<\/span>df3 = pd. <span style=\"color: #3366ff;\">concat<\/span> ([df1, df2], axis= <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#view resulting DataFrame\n<\/span>df3\n\n\tteam points assists rebounds\n0 to 99 to 22\n1 B 91 B 19\n2 C 104 C 25\n3 D 88 D 33\n4 E 108 E 29<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Nota che questo DataFrame corrisponde a quello ottenuto nell&#8217;esempio precedente.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni in Python:<\/span><\/p>\n<p> Come unire due DataFrames Panda su index<br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/\" target=\"_blank\" rel=\"noopener\">Come unire Pandas DataFrames su pi\u00f9 colonne<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/ricerca-del-panda\/\" target=\"_blank\" rel=\"noopener\">Come eseguire una CERCA.VERT in Panda<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>La funzione cbind in R, abbreviazione di column-bind , pu\u00f2 essere utilizzata per combinare insieme i frame di dati in base alle loro colonne. Possiamo usare la funzione pandas concat() per eseguire la funzione equivalente in Python: df3 = pd. concat ([df1, df2], axis= 1 ) I seguenti esempi mostrano come utilizzare questa funzione nella [&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>Come usare cbind in Python (equivalente a R) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come eseguire l&#039;associazione di colonne in Python, equivalente alla funzione cbind in R.\" \/>\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\/cbind-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come usare cbind in Python (equivalente a R) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come eseguire l&#039;associazione di colonne in Python, equivalente alla funzione cbind in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/cbind-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-22T15:31:21+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\/cbind-in-python\/\",\"url\":\"https:\/\/statorials.org\/it\/cbind-in-python\/\",\"name\":\"Come usare cbind in Python (equivalente a R) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-22T15:31:21+00:00\",\"dateModified\":\"2023-07-22T15:31:21+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come eseguire l&#39;associazione di colonne in Python, equivalente alla funzione cbind in R.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/cbind-in-python\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/cbind-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/cbind-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come usare cbind in python (equivalente a r)\"}]},{\"@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":"Come usare cbind in Python (equivalente a R) \u2013 Statorials","description":"Questo tutorial spiega come eseguire l&#39;associazione di colonne in Python, equivalente alla funzione cbind in R.","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\/cbind-in-python\/","og_locale":"it_IT","og_type":"article","og_title":"Come usare cbind in Python (equivalente a R) \u2013 Statorials","og_description":"Questo tutorial spiega come eseguire l&#39;associazione di colonne in Python, equivalente alla funzione cbind in R.","og_url":"https:\/\/statorials.org\/it\/cbind-in-python\/","og_site_name":"Statorials","article_published_time":"2023-07-22T15:31:21+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\/cbind-in-python\/","url":"https:\/\/statorials.org\/it\/cbind-in-python\/","name":"Come usare cbind in Python (equivalente a R) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-22T15:31:21+00:00","dateModified":"2023-07-22T15:31:21+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come eseguire l&#39;associazione di colonne in Python, equivalente alla funzione cbind in R.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/cbind-in-python\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/cbind-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/cbind-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come usare cbind in python (equivalente a r)"}]},{"@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\/2355"}],"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=2355"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2355\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}