{"id":1732,"date":"2023-07-25T05:15:38","date_gmt":"2023-07-25T05:15:38","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/"},"modified":"2023-07-25T05:15:38","modified_gmt":"2023-07-25T05:15:38","slug":"i-panda-combinano-due-colonne","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/","title":{"rendered":"Come combinare due colonne in pandas (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi per combinare due colonne di testo in una in un DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">new_column<\/span> '] = df[' <span style=\"color: #ff0000;\">column1<\/span> '] + df[' <span style=\"color: #ff0000;\">column2<\/span> ']\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se una delle colonne non \u00e8 gi\u00e0 una stringa, puoi convertirla utilizzando il comando <strong>astype(str)<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">new_column<\/span> '] = df[' <span style=\"color: #ff0000;\">column1<\/span> ']. <span style=\"color: #3366ff;\">astype<\/span> ( <span style=\"color: #008000;\">str<\/span> )+df[' <span style=\"color: #ff0000;\">column2<\/span> ']<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">E puoi utilizzare la seguente sintassi per combinare pi\u00f9 colonne di testo in una:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">new_column<\/span> '] = df[[' <span style=\"color: #ff0000;\">col1<\/span> ', ' <span style=\"color: #ff0000;\">col2<\/span> ', ' <span style=\"color: #ff0000;\">col3<\/span> ', ...]]. <span style=\"color: #3366ff;\">agg<\/span> (' '. <span style=\"color: #3366ff;\">join<\/span> , axis= <span style=\"color: #008000;\">1<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come combinare nella pratica le colonne di testo.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: combina due colonne<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come combinare due colonne di testo in una in un 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;\">#create dataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],\n                   ' <span style=\"color: #ff0000;\">first<\/span> ': ['Dirk', 'Kobe', 'Tim', 'Lebron'],\n                   ' <span style=\"color: #ff0000;\">last<\/span> ': ['Nowitzki', 'Bryant', 'Duncan', 'James'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [26, 31, 22, 29]})\n\n<span style=\"color: #008080;\">#combine first and last name column into new column, with space in between<\/span>\ndf[' <span style=\"color: #ff0000;\">full_name<\/span> '] = df[' <span style=\"color: #ff0000;\">first<\/span> '] + ' ' + df[' <span style=\"color: #ff0000;\">last<\/span> ']\n\n<span style=\"color: #008080;\">#view resulting dataFrame\n<span style=\"color: #000000;\">df<\/span>\n\n<span style=\"color: #000000;\">team first last<\/span> <span style=\"color: #000000;\">points full_name\n0 Mavs Dirk Nowitzki<\/span> <span style=\"color: #000000;\">26 Dirk Nowitzki\n1 Lakers Kobe Bryant<\/span> <span style=\"color: #000000;\">31 Kobe Bryant\n2 Spurs Tim Duncan<\/span> <span style=\"color: #000000;\">22 Tim Duncan\n3 Cavs LeBron James<\/span> <span style=\"color: #000000;\">29 LeBron James\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Abbiamo unito la colonna del nome e del cognome con uno spazio in mezzo, ma potremmo anche utilizzare un separatore diverso come un trattino:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#combine first and last name column into new column, with dash in between<\/span>\ndf[' <span style=\"color: #ff0000;\">full_name<\/span> '] = df[' <span style=\"color: #ff0000;\">first<\/span> '] + ' <span style=\"color: #ff0000;\">-<\/span> ' + df[' <span style=\"color: #ff0000;\">last<\/span> ']\n\n<span style=\"color: #008080;\">#view resulting dataFrame\n<span style=\"color: #000000;\">df<\/span>\n\n<span style=\"color: #000000;\">team first last<\/span> <span style=\"color: #000000;\">points full_name\n0 Mavs Dirk Nowitzki<\/span> <span style=\"color: #000000;\">26 Dirk<\/span> - <span style=\"color: #000000;\">Nowitzki\n1 Lakers Kobe Bryant<\/span> <span style=\"color: #000000;\">31 Kobe<\/span> - <span style=\"color: #000000;\">Bryant\n2 Spurs Tim Duncan<\/span> <span style=\"color: #000000;\">22 Tim<\/span> - <span style=\"color: #000000;\">Duncan\n3 Cavs Lebron James<\/span> <span style=\"color: #000000;\">29 Lebron<\/span> - <span style=\"color: #000000;\">James<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: converti in testo e combina due colonne<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come convertire una colonna in testo e quindi unirla a un&#8217;altra colonna:<\/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;\">#create dataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],\n                   ' <span style=\"color: #ff0000;\">first<\/span> ': ['Dirk', 'Kobe', 'Tim', 'Lebron'],\n                   ' <span style=\"color: #ff0000;\">last<\/span> ': ['Nowitzki', 'Bryant', 'Duncan', 'James'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [26, 31, 22, 29]})\n\n<span style=\"color: #008080;\">#convert points to text, then join to last name column<\/span>\ndf[' <span style=\"color: #ff0000;\">name_points<\/span> '] = df[' <span style=\"color: #ff0000;\">last<\/span> '] + df[' <span style=\"color: #ff0000;\">points<\/span> ']. <span style=\"color: #3366ff;\">astype<\/span> ( <span style=\"color: #008000;\">str<\/span> )\n\n<span style=\"color: #008080;\">#view resulting dataFrame\n<span style=\"color: #000000;\">df<\/span>\n\n        <span style=\"color: #000000;\">team first last<\/span> <span style=\"color: #000000;\">points name_points\n0 Mavs Dirk Nowitzki<\/span> <span style=\"color: #000000;\">26 Nowitzki26\n1 Lakers Kobe Bryant<\/span> <span style=\"color: #000000;\">31 Bryant31\n2 Spurs Tim Duncan<\/span> <span style=\"color: #000000;\">22 Duncan22\n3 Cavs LeBron James<\/span> <span style=\"color: #000000;\">29 James29<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 3: combina pi\u00f9 di due colonne<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come unire pi\u00f9 colonne in una:<\/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;\">#create dataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],\n                   ' <span style=\"color: #ff0000;\">first<\/span> ': ['Dirk', 'Kobe', 'Tim', 'Lebron'],\n                   ' <span style=\"color: #ff0000;\">last<\/span> ': ['Nowitzki', 'Bryant', 'Duncan', 'James'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [26, 31, 22, 29]})\n\n<span style=\"color: #008080;\">#join team, first name, and last name into one column<\/span>\ndf[' <span style=\"color: #ff0000;\">team_and_name<\/span> '] = df[[' <span style=\"color: #ff0000;\">team<\/span> ', ' <span style=\"color: #ff0000;\">first<\/span> ', ' <span style=\"color: #ff0000;\">last<\/span> ']]. <span style=\"color: #3366ff;\">agg<\/span> (' '. <span style=\"color: #3366ff;\">join<\/span> , axis= <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#view resulting dataFrame\n<span style=\"color: #000000;\">df<\/span>\n\n<span style=\"color: #000000;\">team first last points team_name\n0 Mavs Dirk Nowitzki 26 Mavs Dirk Nowitzki\n1 Lakers Kobe Bryant 31 Lakers Kobe Bryant\n2 Spurs Tim Duncan 22 Spurs Tim Duncan\n3 Cavs Lebron James 29 Cavs Lebron James<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/it\/differenza-dei-panda-tra-due-colonne\/\" target=\"_blank\" rel=\"noopener\">Panda: come trovare la differenza tra due colonne<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/differenza-dei-panda-tra-le-righe\/\" target=\"_blank\" rel=\"noopener\">Panda: come trovare la differenza tra due linee<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-ordinano-le-colonne-per-nome\/\" target=\"_blank\" rel=\"noopener\">Panda: come ordinare le colonne per nome<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi per combinare due colonne di testo in una in un DataFrame panda: df[&#8216; new_column &#8216;] = df[&#8216; column1 &#8216;] + df[&#8216; column2 &#8216;] Se una delle colonne non \u00e8 gi\u00e0 una stringa, puoi convertirla utilizzando il comando astype(str) : df[&#8216; new_column &#8216;] = df[&#8216; column1 &#8216;]. astype ( str [&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 combinare due colonne in Pandas (con esempi)<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi 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-combinano-due-colonne\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come combinare due colonne in Pandas (con esempi)\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-25T05:15:38+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=\"3 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-combinano-due-colonne\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/\",\"name\":\"Come combinare due colonne in Pandas (con esempi)\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-25T05:15:38+00:00\",\"dateModified\":\"2023-07-25T05:15:38+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come combinare due colonne in pandas (con esempi)\"}]},{\"@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 combinare due colonne in Pandas (con esempi)","description":"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi 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-combinano-due-colonne\/","og_locale":"it_IT","og_type":"article","og_title":"Come combinare due colonne in Pandas (con esempi)","og_description":"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/","og_site_name":"Statorials","article_published_time":"2023-07-25T05:15:38+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/","url":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/","name":"Come combinare due colonne in Pandas (con esempi)","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-25T05:15:38+00:00","dateModified":"2023-07-25T05:15:38+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come combinare due colonne in un DataFrame panda, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-combinano-due-colonne\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come combinare due colonne in pandas (con esempi)"}]},{"@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\/1732"}],"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=1732"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/1732\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=1732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=1732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=1732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}