{"id":4132,"date":"2023-07-13T09:43:42","date_gmt":"2023-07-13T09:43:42","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/"},"modified":"2023-07-13T09:43:42","modified_gmt":"2023-07-13T09:43:42","slug":"i-panda-uniscono-i-file-csv","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/","title":{"rendered":"Come unire pi\u00f9 file csv in panda (con esempio)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi di base per unire pi\u00f9 file CSV situati nella stessa cartella in un DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n<span style=\"color: #008000;\">import<\/span> glob\n<span style=\"color: #008000;\">import<\/span> bone\n\n<span style=\"color: #008080;\">#define path to CSV files\n<\/span>path = r' <span style=\"color: #ff0000;\">C:\\Users\\bob\\Documents\\my_data_files<\/span> '\n\n<span style=\"color: #008080;\">#identify all CSV files\n<\/span>all_files = glob. <span style=\"color: #3366ff;\">glob<\/span> ( <span style=\"color: #3366ff;\">os.path.join<\/span> (\" <span style=\"color: #ff0000;\">*.csv<\/span> <span style=\"color: #3366ff;\">\"<\/span> ))\n\n<span style=\"color: #008080;\">#merge all CSV files into one DataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">concat<\/span> ((pd. <span style=\"color: #3366ff;\">read_csv<\/span> (f) <span style=\"color: #008000;\">for<\/span> f <span style=\"color: #008000;\">in<\/span> all_files), ignore_index= <span style=\"color: #008000;\">True<\/span> )\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo particolare esempio unir\u00e0 tutti i file CSV che si trovano nella cartella denominata <strong>my_data_files<\/strong> in un singolo Pandas DataFrame.<\/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: unisci pi\u00f9 file CSV in Panda<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Diciamo che ho una cartella sul mio computer chiamata <strong>my_data_files<\/strong> che contiene tre file CSV:<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-26328 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fusioncsv1.jpg\" alt=\"\" width=\"582\" height=\"465\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Ogni file CSV contiene due colonne denominate <strong>punti<\/strong> e <strong>assist<\/strong> , che rappresentano i punti e gli assist di diversi giocatori di basket.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ecco come appare il primo CSV chiamato <strong>df1<\/strong> :<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-32834 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fusionner1.png\" alt=\"\" width=\"520\" height=\"482\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per unire i tre file CSV nella cartella in un unico DataFrame panda:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n<span style=\"color: #008000;\">import<\/span> glob\n<span style=\"color: #008000;\">import<\/span> bone\n\n<span style=\"color: #008080;\">#define path to CSV files\n<\/span>path = r' <span style=\"color: #ff0000;\">C:\\Users\\bob\\Documents\\my_data_files<\/span> '\n\n<span style=\"color: #008080;\">#identify all CSV files\n<\/span>all_files = glob. <span style=\"color: #3366ff;\">glob<\/span> ( <span style=\"color: #3366ff;\">os.path.join<\/span> (\" <span style=\"color: #ff0000;\">*.csv<\/span> <span style=\"color: #3366ff;\">\"<\/span> ))\n\n<span style=\"color: #008080;\">#merge all CSV files into one DataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">concat<\/span> ((pd. <span style=\"color: #3366ff;\">read_csv<\/span> (f) <span style=\"color: #008000;\">for<\/span> f <span style=\"color: #008000;\">in<\/span> all_files), ignore_index= <span style=\"color: #008000;\">True<\/span> )\n\n<span style=\"color: #008080;\">#view resulting DataFrame<\/span>\n<span style=\"color: #008000;\">print<\/span> (df)\n\n    assist points\n0 4 3\n1 5 2\n2 5 4\n3 6 4\n4 8 6\n5 9 3\n6 2 3\n7 10 2\n8 14 9\n9 15 3\n10 6 10\n11 8 6\n12 9 4\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che tutti e tre i file CSV sono stati importati e uniti con successo in un singolo DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Il DataFrame finale contiene 13 righe e 2 colonne.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa della funzione panda <strong>read_csv()<\/strong> <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.read_csv.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 altre attivit\u00e0 comuni in Python:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/i-panda-saltano-di-classifica\/\" target=\"_blank\" rel=\"noopener\">Panda: come saltare le righe durante la lettura di un file CSV<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-hanno-aggiunto-csv\/\" target=\"_blank\" rel=\"noopener\">Panda: come aggiungere dati a un file CSV esistente<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-il-dtype-csv\/\" target=\"_blank\" rel=\"noopener\">Panda: come specificare i tipi durante l&#8217;importazione di un file CSV<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-la-colonna-del-nome-csv\/\" target=\"_blank\" rel=\"noopener\">Panda: come impostare i nomi delle colonne durante l&#8217;importazione di un file CSV<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi di base per unire pi\u00f9 file CSV situati nella stessa cartella in un DataFrame panda: import pandas as pd import glob import bone #define path to CSV files path = r&#8217; C:\\Users\\bob\\Documents\\my_data_files &#8216; #identify all CSV files all_files = glob. glob ( os.path.join (&#8221; *.csv &#8221; )) #merge all [&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 pi\u00f9 file CSV in Panda (con esempio) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, 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-i-file-csv\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come unire pi\u00f9 file CSV in Panda (con esempio) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T09:43:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fusioncsv1.jpg\" \/>\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-i-file-csv\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/\",\"name\":\"Come unire pi\u00f9 file CSV in Panda (con esempio) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-13T09:43:42+00:00\",\"dateModified\":\"2023-07-13T09:43:42+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come unire pi\u00f9 file csv in panda (con esempio)\"}]},{\"@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 pi\u00f9 file CSV in Panda (con esempio) - Statorials","description":"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, 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-i-file-csv\/","og_locale":"it_IT","og_type":"article","og_title":"Come unire pi\u00f9 file CSV in Panda (con esempio) - Statorials","og_description":"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, con un esempio.","og_url":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/","og_site_name":"Statorials","article_published_time":"2023-07-13T09:43:42+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fusioncsv1.jpg"}],"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-i-file-csv\/","url":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/","name":"Come unire pi\u00f9 file CSV in Panda (con esempio) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-13T09:43:42+00:00","dateModified":"2023-07-13T09:43:42+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come unire pi\u00f9 file CSV in panda, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-uniscono-i-file-csv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come unire pi\u00f9 file csv in panda (con esempio)"}]},{"@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\/4132"}],"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=4132"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4132\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}