{"id":893,"date":"2023-07-28T09:54:10","date_gmt":"2023-07-28T09:54:10","guid":{"rendered":"https:\/\/statorials.org\/it\/panda-a-json\/"},"modified":"2023-07-28T09:54:10","modified_gmt":"2023-07-28T09:54:10","slug":"panda-a-json","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/panda-a-json\/","title":{"rendered":"Come convertire un dataframe pandas in json"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti essere interessato a convertire un DataFrame Panda in formato JSON.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Fortunatamente, questo \u00e8 facile da fare utilizzando la<\/span> funzione <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.to_json.html\" target=\"_blank\" rel=\"noopener\">to_json()<\/a> <span style=\"color: #000000;\">, che consente di convertire un DataFrame in una stringa JSON con uno dei seguenti formati:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>&#8216;dividi&#8217;:<\/strong> dict come {&#8216;indice&#8217; -&gt; [indice], &#8216;colonne&#8217; -&gt; [colonne], &#8216;dati&#8217; -&gt; [valori]}<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>&#8216;record&#8217;:<\/strong> elenco come [{colonna -&gt; valore}, &#8230;, {colonna -&gt; valore}]<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>&#8216;indice&#8217;:<\/strong> dict come {indice -&gt; {colonna -&gt; valore}}<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>&#8216;colonne&#8217;:<\/strong> dict come {colonna -&gt; {indice -&gt; valore}}<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>&#8216;valori&#8217;:<\/strong> solo l&#8217;array di valori<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>&#8216;tabella&#8217;:<\/strong> dict come {&#8216;schema&#8217;: {schema}, &#8216;dati&#8217;: {dati}}<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Questo tutorial mostra come convertire un DataFrame in ciascuno dei sei formati utilizzando il seguente DataFrame 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;\">#createDataFrame<\/span>\ndf = pd.DataFrame({'points': [25, 12, 15, 19],\n                   'assists': [5, 7, 7, 12]})  \n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span>df\n\n        assist points\n0 25 5\n1 12 7\n2 15 7\n3 19 12\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 1: \u201cDividi\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">split<\/span> ')\n\n{\n   \"columns\": [\n      \"points\",\n      \"assists\"\n   ],\n   \"index\": [\n      0,\n      1,\n      2,\n      3\n   ],\n   \"data\": [\n      [\n         25,\n         5\n      ],\n      [\n         12,\n         7\n      ],\n      [\n         15,\n         7\n      ],\n      [\n         19,\n         12\n      ]\n   ]\n}\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 2: \u201cRegistrazioni\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">records<\/span> ')\n\n[\n   {\n      \"points\": 25,\n      \u201cassists\u201d: 5\n   },\n   {\n      \"points\": 12,\n      \u201cassists\u201d: 7\n   },\n   {\n      \"points\": 15,\n      \u201cassists\u201d: 7\n   },\n   {\n      \"points\": 19,\n      \u201cassists\u201d: 12\n   }\n]<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 3: \u201cIndice\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">index<\/span> ') \n\n{\n   \"0\": {\n      \"points\": 25,\n      \u201cassists\u201d: 5\n   },\n   \"1\": {\n      \"points\": 12,\n      \u201cassists\u201d: 7\n   },\n   \"2\": {\n      \"points\": 15,\n      \u201cassists\u201d: 7\n   },\n   \"3\": {\n      \"points\": 19,\n      \u201cassists\u201d: 12\n   }\n}<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 4: \u201cColonne\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">columns<\/span> ') \n\n{\n   \"dots\": {\n      \"0\": 25,\n      \"1\": 12,\n      \"2\": 15,\n      \"3\": 19\n   },\n   \"assists\": {\n      \"0\": 5,\n      \"1\": 7,\n      \"2\": 7,\n      \"3\": 12\n   }\n}\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 5: \u201cValori\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">values<\/span> ') \n\n[\n   [\n      25,\n      5\n   ],\n   [\n      12,\n      7\n   ],\n   [\n      15,\n      7\n   ],\n   [\n      19,\n      12\n   ]\n]<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Metodo 6: \u201cTabella\u201d<\/strong><\/span><\/h3>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">table<\/span> ') \n\n{\n   \"plan\": {\n      \"fields\": [\n         {\n            \"name\": \"index\",\n            \"type\": \"integer\"\n         },\n         {\n            \"name\": \"points\",\n            \"type\": \"integer\"\n         },\n         {\n            \"name\": \"assists\",\n            \"type\": \"integer\"\n         }\n      ],\n      \"primaryKey\": [\n         \"index\"\n      ],\n      \"pandas_version\": \"0.20.0\"\n   },\n   \"data\": [\n      {\n         \"index\": 0,\n         \"points\": 25,\n         \u201cassists\u201d: 5\n      },\n      {\n         \"index\": 1,\n         \"points\": 12,\n         \u201cassists\u201d: 7\n      },\n      {\n         \"index\": 2,\n         \"points\": 15,\n         \u201cassists\u201d: 7\n      },\n      {\n         \"index\": 3,\n         \"points\": 19,\n         \u201cassists\u201d: 12\n      }\n   ]\n}<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Come esportare un file JSON<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Puoi utilizzare la seguente sintassi per esportare un file JSON in un percorso file specifico sul tuo computer:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create JSON file<\/span> \njson_file = df. <span style=\"color: #3366ff;\">to_json<\/span> (orient=' <span style=\"color: #008000;\">records<\/span> ') \n\n<span style=\"color: #008080;\">#export JSON file<\/span>\nwith open('my_data.json', 'w') as f:\n    f.write(json_file)\n<\/strong><\/pre>\n<p> <em><span style=\"color: #000000;\">Puoi trovare la documentazione completa della funzione panda to_json()<\/span> <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.to_json.html\" target=\"_blank\" rel=\"noopener\">qui<\/a> <span style=\"color: #000000;\">.<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti essere interessato a convertire un DataFrame Panda in formato JSON. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione to_json() , che consente di convertire un DataFrame in una stringa JSON con uno dei seguenti formati: &#8216;dividi&#8217;: dict come {&#8216;indice&#8217; -&gt; [indice], &#8216;colonne&#8217; -&gt; [colonne], &#8216;dati&#8217; -&gt; [valori]} &#8216;record&#8217;: elenco come [{colonna -&gt; [&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 convertire un Pandas DataFrame in JSON - Statorials<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.\" \/>\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-a-json\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come convertire un Pandas DataFrame in JSON - Statorials\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/panda-a-json\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T09:54:10+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\/panda-a-json\/\",\"url\":\"https:\/\/statorials.org\/it\/panda-a-json\/\",\"name\":\"Come convertire un Pandas DataFrame in JSON - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T09:54:10+00:00\",\"dateModified\":\"2023-07-28T09:54:10+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/panda-a-json\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/panda-a-json\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/panda-a-json\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come convertire un dataframe pandas in json\"}]},{\"@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 convertire un Pandas DataFrame in JSON - Statorials","description":"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.","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-a-json\/","og_locale":"it_IT","og_type":"article","og_title":"Come convertire un Pandas DataFrame in JSON - Statorials","og_description":"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.","og_url":"https:\/\/statorials.org\/it\/panda-a-json\/","og_site_name":"Statorials","article_published_time":"2023-07-28T09:54:10+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\/panda-a-json\/","url":"https:\/\/statorials.org\/it\/panda-a-json\/","name":"Come convertire un Pandas DataFrame in JSON - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T09:54:10+00:00","dateModified":"2023-07-28T09:54:10+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione su come convertire un DataFrame Panda in formato JSON.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/panda-a-json\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/panda-a-json\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/panda-a-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come convertire un dataframe pandas in json"}]},{"@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\/893"}],"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=893"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/893\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}