{"id":3952,"date":"2023-07-14T13:41:05","date_gmt":"2023-07-14T13:41:05","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/"},"modified":"2023-07-14T13:41:05","modified_gmt":"2023-07-14T13:41:05","slug":"i-panda-uniscono-le-colonne-con-lo-stesso-nome","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/","title":{"rendered":"Panda: come unire colonne che condividono lo stesso nome"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi di base per unire le colonne in un DataFrame panda che condividono lo stesso nome di colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to merge columns with same names together\n<\/span><span style=\"color: #008000;\">def<\/span> <span style=\"color: #0000ff;\">same_merge<\/span> (x): <span style=\"color: #008000;\">return<\/span> ' <span style=\"color: #ff0000;\">,<\/span> '. <span style=\"color: #3366ff;\">join<\/span> (x[ <span style=\"color: #3366ff;\">x.notnull<\/span> ()]. <span style=\"color: #3366ff;\">astype<\/span> (str))\n\n<span style=\"color: #008080;\">#define new DataFrame that merges columns with same names together\n<\/span>df_new = df. <span style=\"color: #3366ff;\">groupby<\/span> (level= <span style=\"color: #008000;\">0<\/span> , axis= <span style=\"color: #008000;\">1<\/span> ). <span style=\"color: #3366ff;\">apply<\/span> ( <span style=\"color: #008000;\">lambda<\/span> x: <span style=\"color: #3366ff;\">x.apply<\/span> (same_merge,axis= <span style=\"color: #008000;\">1<\/span> ))<\/strong><\/pre>\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: unisci colonne che condividono lo stesso nome in Panda<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti panda DataFrame:<\/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<span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">\nimport<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">A<\/span> ': [5, 6, 8, np.nan, 4, np.nan, np.nan],\n                   ' <span style=\"color: #ff0000;\">A1<\/span> ': [np.nan, 12, np.nan, 10, np.nan, 6, 4],\n                   ' <span style=\"color: #ff0000;\">B<\/span> ': [2, 7, np.nan, np.nan, 2, 4, np.nan],\n                   ' <span style=\"color: #ff0000;\">B1<\/span> ': [5, np.nan, 6, 15, 1, np.nan, 4]})\n\n<span style=\"color: #008080;\">#rename columns so there are duplicate column names\n<\/span>df. <span style=\"color: #3366ff;\">columns<\/span> = [' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> ']\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n     AABB\n0 5.0 NaN 2.0 5.0\n1 6.0 12.0 7.0 NaN\n2 8.0 NaN NaN 6.0\n3 NaN 10.0 NaN 15.0\n4 4.0 NaN 2.0 1.0\n5 NaN 6.0 4.0 NaN\n6 NaN 4.0 NaN 4.0<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che due colonne sono denominate &#8220;A&#8221; e due colonne sono denominate &#8220;B&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per unire colonne che hanno gli stessi nomi di colonna e concatenare i loro valori con una virgola:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to merge columns with same names together\n<\/span><span style=\"color: #008000;\">def<\/span> <span style=\"color: #0000ff;\">same_merge<\/span> (x): <span style=\"color: #008000;\">return<\/span> ' <span style=\"color: #ff0000;\">,<\/span> '. <span style=\"color: #3366ff;\">join<\/span> (x[ <span style=\"color: #3366ff;\">x.notnull<\/span> ()]. <span style=\"color: #3366ff;\">astype<\/span> (str))\n\n<span style=\"color: #008080;\">#define new DataFrame that merges columns with same names together\n<\/span>df_new = df. <span style=\"color: #3366ff;\">groupby<\/span> (level= <span style=\"color: #008000;\">0<\/span> , axis= <span style=\"color: #008000;\">1<\/span> ). <span style=\"color: #3366ff;\">apply<\/span> ( <span style=\"color: #008000;\">lambda<\/span> x: <span style=\"color: #3366ff;\">x.apply<\/span> (same_merge,axis= <span style=\"color: #008000;\">1<\/span> ))\n\n<span style=\"color: #008080;\">#view new DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df_new)\n\n          AB\n0 5.0 2.0,5.0\n1 6.0,12.0 7.0\n2 8.0 6.0\n3 10.0 15.0\n4 4.0 2.0,1.0\n5 6.0 4.0\n6 4.0 4.0<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il nuovo DataFrame ha unito le colonne con gli stessi nomi e ha concatenato i loro valori con una virgola.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se desideri utilizzare un separatore diverso, sostituisci semplicemente il separatore virgola con qualcos&#8217;altro nella funzione <strong>same_merge()<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, il codice seguente mostra come utilizzare invece un separatore punto e virgola:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to merge columns with same names together\n<\/span><span style=\"color: #008000;\">def<\/span> <span style=\"color: #0000ff;\">same_merge<\/span> (x): <span style=\"color: #008000;\">return<\/span> ' <span style=\"color: #ff0000;\">;<\/span> '. <span style=\"color: #3366ff;\">join<\/span> (x[ <span style=\"color: #3366ff;\">x.notnull<\/span> ()]. <span style=\"color: #3366ff;\">astype<\/span> (str))\n\n<span style=\"color: #008080;\">#define new DataFrame that merges columns with same names together\n<\/span>df_new = df. <span style=\"color: #3366ff;\">groupby<\/span> (level= <span style=\"color: #008000;\">0<\/span> , axis= <span style=\"color: #008000;\">1<\/span> ). <span style=\"color: #3366ff;\">apply<\/span> ( <span style=\"color: #008000;\">lambda<\/span> x: <span style=\"color: #3366ff;\">x.apply<\/span> (same_merge,axis= <span style=\"color: #008000;\">1<\/span> ))\n\n<span style=\"color: #008080;\">#view new DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df_new)\n\n          AB\n0 5.0 2.0;5.0\n1 6.0;12.0 7.0\n2 8.0 6.0\n3 10.0 15.0\n4 4.0 2.0;1.0\n5 6.0 4.0\n6 4.0 4.0\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il nuovo DataFrame ha unito le colonne con gli stessi nomi e ha concatenato i loro valori con un punto e virgola.<\/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 operazioni comuni nei panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/i-panda-rilasciano-colonne-duplicate\/\" target=\"_blank\" rel=\"noopener\">Come rimuovere le colonne duplicate in Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/panda-elenca-i-nomi-delle-colonne\/\" target=\"_blank\" rel=\"noopener\">Come elencare tutti i nomi delle colonne in Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-ordinano-le-colonne-per-nome\/\" target=\"_blank\" rel=\"noopener\">Come ordinare le colonne per nome in Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi di base per unire le colonne in un DataFrame panda che condividono lo stesso nome di colonna: #define function to merge columns with same names together def same_merge (x): return &#8216; , &#8216;. join (x[ x.notnull ()]. astype (str)) #define new DataFrame that merges columns with same names together [&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 unire colonne con lo stesso nome - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, 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\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: come unire colonne con lo stesso nome - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-14T13:41:05+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\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/\",\"name\":\"Panda: come unire colonne con lo stesso nome - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-14T13:41:05+00:00\",\"dateModified\":\"2023-07-14T13:41:05+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come unire colonne che condividono lo stesso nome\"}]},{\"@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 unire colonne con lo stesso nome - Statorials","description":"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, 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\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: come unire colonne con lo stesso nome - Statorials","og_description":"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, con un esempio.","og_url":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/","og_site_name":"Statorials","article_published_time":"2023-07-14T13:41:05+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\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/","url":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/","name":"Panda: come unire colonne con lo stesso nome - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-14T13:41:05+00:00","dateModified":"2023-07-14T13:41:05+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come unire le colonne in un DataFrame Panda che condivide lo stesso nome, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-le-colonne-con-lo-stesso-nome\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come unire colonne che condividono lo stesso nome"}]},{"@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\/3952"}],"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=3952"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3952\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}