{"id":4137,"date":"2023-07-13T08:51:26","date_gmt":"2023-07-13T08:51:26","guid":{"rendered":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/"},"modified":"2023-07-13T08:51:26","modified_gmt":"2023-07-13T08:51:26","slug":"panda-in-un-file-di-testo","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/","title":{"rendered":"Come esportare pandas dataframe in un file di testo"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi per esportare un DataFrame panda in un file di testo:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#specify path for export<\/span>\npath = r' <span style=\"color: #ff0000;\">c:\\data_folder\\my_data.txt<\/span> '\n\n<span style=\"color: #008080;\">#export DataFrame to text file\n<\/span><span style=\"color: #008000;\">with open<\/span> (path, ' <span style=\"color: #ff0000;\">a<\/span> ') <span style=\"color: #008000;\">as<\/span> f:\n    df_string = df. <span style=\"color: #3366ff;\">to_string<\/span> (header= <span style=\"color: #008000;\">False<\/span> , index= <span style=\"color: #008000;\">False<\/span> )\n    f. <span style=\"color: #3366ff;\">write<\/span> (df_string)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">L&#8217;argomento <strong>header=False<\/strong> dice ai panda di non includere la riga di intestazione nel file di testo e <strong>index=False<\/strong> dice ai panda di non includere la colonna dell&#8217;indice nel file di testo.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">Sentiti libero di omettere questi argomenti se desideri includere la riga di intestazione o la colonna di indice nel file di testo.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare in pratica questa sintassi per esportare un DataFrame panda in un file di testo.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: esporta Pandas DataFrame in un file di testo<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente DataFrame panda che contiene informazioni su vari giocatori di basket:<\/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;\">#createDataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n                   ' <span style=\"color: #ff0000;\">points<\/span> ': [18, 22, 19, 14, 14, 11, 20, 28],\n                   ' <span style=\"color: #ff0000;\">assists<\/span> ': [5, 7, 7, 9, 12, 9, 9, 4],\n                   ' <span style=\"color: #ff0000;\">rebounds<\/span> ': [11, 8, 10, 6, 6, 5, 9, 12]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points assists rebounds\n0 A 18 5 11\n1 B 22 7 8\n2 C 19 7 10\n3 D 14 9 6\n4 E 14 12 6\n5 F 11 9 5\n6 G 20 9 9\n7:28 4 12<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per esportare questo DataFrame in un file di testo chiamato <strong>basket_data.txt<\/strong> :<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#specify path for export<\/span>\npath = r' <span style=\"color: #ff0000;\">c:\\data_folder\\basketball_data.txt<\/span> '\n\n<span style=\"color: #008080;\">#export DataFrame to text file\n<\/span><span style=\"color: #008000;\">with open<\/span> (path, ' <span style=\"color: #ff0000;\">a<\/span> ') <span style=\"color: #008000;\">as<\/span> f:\n    df_string = df. <span style=\"color: #3366ff;\">to_string<\/span> (header= <span style=\"color: #008000;\">False<\/span> , index= <span style=\"color: #008000;\">False<\/span> )\n    f. <span style=\"color: #3366ff;\">write<\/span> (df_string)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se navigo nella cartella in cui ho esportato questo file, posso visualizzare il file di testo:<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-32859 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fichier-texte1.png\" alt=\"\" width=\"410\" height=\"377\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">I valori nel file di testo corrispondono ai valori nel DataFrame panda.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che la riga di intestazione e la colonna di indice sono state entrambe rimosse dal DataFrame, come specificato.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se desideri mantenere la riga di intestazione e la colonna di indice nel file di testo, puoi utilizzare la seguente sintassi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#specify path for export<\/span>\npath = r' <span style=\"color: #ff0000;\">c:\\data_folder\\basketball_data.txt<\/span> '\n\n<span style=\"color: #008080;\">#export DataFrame to text file (keep header row and index column)\n<\/span><span style=\"color: #008000;\">with open<\/span> (path, ' <span style=\"color: #ff0000;\">a<\/span> ') <span style=\"color: #008000;\">as<\/span> f:\n    df_string = df. <span style=\"color: #3366ff;\">to_string<\/span> ()\n    f. <span style=\"color: #3366ff;\">write<\/span> (df_string)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se navigo nella cartella in cui ho esportato questo file, posso visualizzare il file di testo:<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-32860 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fichier-texte2.png\" alt=\"\" width=\"443\" height=\"368\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che sia la riga di intestazione che la colonna di indice sono incluse nel file di testo.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Correlato:<\/strong> <a href=\"https:\/\/statorials.org\/it\/con-pitone-aperto\/\" target=\"_blank\" rel=\"noopener\">Come usare \u201cwith open\u201d in Python<\/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 nei panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/panda-in-csv\/\" target=\"_blank\" rel=\"noopener\">Come esportare Pandas DataFrame in CSV<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/esportare-il-dataframe-panda-in-excel\/\" target=\"_blank\" rel=\"noopener\">Come esportare un DataFrame Pandas in Excel<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/panda-a-json\/\" target=\"_blank\" rel=\"noopener\">Come esportare un DataFrame Pandas in JSON<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi per esportare un DataFrame panda in un file di testo: #specify path for export path = r&#8217; c:\\data_folder\\my_data.txt &#8216; #export DataFrame to text file with open (path, &#8216; a &#8216;) as f: df_string = df. to_string (header= False , index= False ) f. write (df_string) L&#8217;argomento header=False dice ai [&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 esportare Pandas DataFrame in un file di testo - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, 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\/panda-in-un-file-di-testo\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come esportare Pandas DataFrame in un file di testo - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T08:51:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fichier-texte1.png\" \/>\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\/panda-in-un-file-di-testo\/\",\"url\":\"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/\",\"name\":\"Come esportare Pandas DataFrame in un file di testo - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-13T08:51:26+00:00\",\"dateModified\":\"2023-07-13T08:51:26+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come esportare pandas dataframe in un file di testo\"}]},{\"@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 esportare Pandas DataFrame in un file di testo - Statorials","description":"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, 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\/panda-in-un-file-di-testo\/","og_locale":"it_IT","og_type":"article","og_title":"Come esportare Pandas DataFrame in un file di testo - Statorials","og_description":"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, con un esempio.","og_url":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/","og_site_name":"Statorials","article_published_time":"2023-07-13T08:51:26+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/fichier-texte1.png"}],"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\/panda-in-un-file-di-testo\/","url":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/","name":"Come esportare Pandas DataFrame in un file di testo - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-13T08:51:26+00:00","dateModified":"2023-07-13T08:51:26+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come esportare un DataFrame Panda in un file di testo, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/panda-in-un-file-di-testo\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come esportare pandas dataframe in un file di testo"}]},{"@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\/4137"}],"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=4137"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4137\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}