{"id":975,"date":"2023-07-28T03:15:43","date_gmt":"2023-07-28T03:15:43","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/"},"modified":"2023-07-28T03:15:43","modified_gmt":"2023-07-28T03:15:43","slug":"i-panda-uniscono-piu-colonne","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/","title":{"rendered":"Come unire pandas dataframes su pi\u00f9 colonne"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti voler unire due DataFrames panda su pi\u00f9 colonne. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione pandas <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.merge.html\" target=\"_blank\" rel=\"noopener noreferrer\">merge()<\/a> , che utilizza la seguente sintassi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>p.d. <span style=\"color: #3366ff;\">merge<\/span> (df1, df2, left_on=['col1','col2'], right_on = ['col1','col2'])<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo tutorial spiega come utilizzare questa funzione nella pratica.<\/span><\/p>\n<h3> <strong>Esempio 1: <span style=\"color: #000000;\">unisci<\/span> pi\u00f9 colonne con nomi diversi<\/strong><\/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: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#create and view first DataFrame<\/span>\ndf1 = pd.DataFrame({'a1': [0, 0, 1, 1, 2],\n                   'b': [0, 0, 1, 1, 1],\n                   'c': [11, 8, 10, 6, 6]})\n\n<span style=\"color: #3366ff;\">print<\/span> (df1)\n\n   a1 bc\n0 0 0 11\n1 0 0 8\n2 1 1 10\n3 1 1 6\n4 2 1 6\n\n<span style=\"color: #008080;\">#create and view second DataFrame<\/span> \ndf2 = pd.DataFrame({'a2': [0, 1, 1, 1, 3],\n                   'b': [0, 0, 0, 1, 1],\n                   'd': [22, 24, 25, 33, 37]})\n\n<span style=\"color: #3366ff;\">print<\/span> (df2)\n\n   a2 comic\n0 0 0 22\n1 1 0 24\n2 1 0 25\n3 1 1 33\n4 3 1 37\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come eseguire un left join utilizzando pi\u00f9 colonne da entrambi i DataFrames:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>p.d. <span style=\"color: #3366ff;\">merge<\/span> (df1, df2, how=' <span style=\"color: #008000;\">left<\/span> ', left_on=[' <span style=\"color: #008000;\">a1<\/span> ', ' <span style=\"color: #008000;\">b<\/span> '], right_on = [' <span style=\"color: #008000;\">a2<\/span> ', ' <span style=\"color: #008000;\">b<\/span> '])\n\n\n        a1 b c a2 d\n0 0 0 11 0.0 22.0\n1 0 0 8 0.0 22.0\n2 1 1 10 1.0 33.0\n3 1 1 6 1.0 33.0\n4 2 1 6 NaN NaN\n<\/strong><\/pre>\n<h3> <strong>Esempio 2: <span style=\"color: #000000;\">unisci<\/span> pi\u00f9 colonne con gli stessi nomi<\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti due DataFrames panda con gli stessi nomi di colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrames<\/span>\ndf1 = pd.DataFrame({'a': [0, 0, 1, 1, 2],\n                   'b': [0, 0, 1, 1, 1],\n                   'c': [11, 8, 10, 6, 6]})\n\ndf2 = pd.DataFrame({'a': [0, 1, 1, 1, 3],\n                   'b': [0, 0, 0, 1, 1],\n                   'd': [22, 24, 25, 33, 37]})\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">In questo caso, possiamo semplificare utilizzando <strong>on = [&#8216;a&#8217;, &#8216;b&#8217;]<\/strong> poich\u00e9 i nomi delle colonne sono gli stessi in entrambi i DataFrames:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>p.d. <span style=\"color: #3366ff;\">merge<\/span> (df1, df2, how=' <span style=\"color: #008000;\">left<\/span> ', on=[' <span style=\"color: #008000;\">a<\/span> ', ' <span style=\"color: #008000;\">b<\/span> '])\n\n\ta b c d\n0 0 0 11 22.0\n1 0 0 8 22.0\n2 1 1 10 33.0\n3 1 1 6 33.0\n4 2 1 6 NaN\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> Come unire due DataFrames Panda su index<br \/> <a href=\"https:\/\/statorials.org\/it\/impilare-i-dataframe-dei-panda\/\" target=\"_blank\" rel=\"noopener noreferrer\">Come impilare pi\u00f9 DataFrames Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti voler unire due DataFrames panda su pi\u00f9 colonne. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione pandas merge() , che utilizza la seguente sintassi: p.d. merge (df1, df2, left_on=[&#8216;col1&#8242;,&#8217;col2&#8217;], right_on = [&#8216;col1&#8242;,&#8217;col2&#8217;]) Questo tutorial spiega come utilizzare questa funzione nella pratica. Esempio 1: unisci pi\u00f9 colonne con nomi diversi Supponiamo di avere [&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 unire Pandas DataFrames su pi\u00f9 colonne - Statorials<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.\" \/>\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\/i-panda-uniscono-piu-colonne\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come unire Pandas DataFrames su pi\u00f9 colonne - Statorials\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T03:15:43+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=\"1 minuto\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/\",\"name\":\"Come unire Pandas DataFrames su pi\u00f9 colonne - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T03:15:43+00:00\",\"dateModified\":\"2023-07-28T03:15:43+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come unire pandas dataframes su pi\u00f9 colonne\"}]},{\"@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 unire Pandas DataFrames su pi\u00f9 colonne - Statorials","description":"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.","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\/i-panda-uniscono-piu-colonne\/","og_locale":"it_IT","og_type":"article","og_title":"Come unire Pandas DataFrames su pi\u00f9 colonne - Statorials","og_description":"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.","og_url":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/","og_site_name":"Statorials","article_published_time":"2023-07-28T03:15:43+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"1 minuto"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/","url":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/","name":"Come unire Pandas DataFrames su pi\u00f9 colonne - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T03:15:43+00:00","dateModified":"2023-07-28T03:15:43+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione su come unire due DataFrames Panda su pi\u00f9 colonne, inclusi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-piu-colonne\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come unire pandas dataframes su pi\u00f9 colonne"}]},{"@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\/975"}],"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=975"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/975\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}